SQLiteDatabase.Update C# (CSharp) Method

Update() public method

Allows the programmer to easily update rows in the DB.
public Update ( String tableName, String>.Dictionary data, String where ) : bool
tableName String The table to update.
data String>.Dictionary A dictionary containing Column names and their new values.
where String The where clause for the update statement.
return bool
    public bool Update(String tableName, Dictionary<String, String> data, String where)
    {
        String vals = "";
        Boolean returnCode = true;
        if (data.Count >= 1)
        {
            foreach (KeyValuePair<String, String> val in data)
            {
                if (val.Value.ToString() == "current_timestamp")
                    vals += String.Format(" {0} = {1},", val.Key.ToString(), val.Value.ToString());
                else
                    vals += String.Format(" {0} = '{1}',", val.Key.ToString(), val.Value.ToString());
            }
            vals = vals.Substring(0, vals.Length - 1);
        }
        try
        {
            this.ExecuteNonQuery(String.Format("update {0} set {1} where {2};", tableName, vals, where));
        }
        catch
        {
            returnCode = false;
        }
        return returnCode;
    }

Usage Example

            /**
             * Handle updating data.
             */
            public override int Update(Uri uri, Android_Content.ContentValues values, string where, string[] whereArgs)
            {
                SQLiteDatabase db = mOpenHelper.GetWritableDatabase();
                int count;
                string constWhere;

                switch (mUriMatcher.Match(uri)) {
                    case MAIN:
                        // If URI is main table, update uses incoming where clause and args.
                        count = db.Update(MainTable.TABLE_NAME, values, where, whereArgs);
                        break;

                    case MAIN_ID:
                        // If URI is for a particular row ID, update is based on incoming
                        // data but modified to restrict to the given ID.
                        constWhere = DatabaseUtilsCompat.ConcatenateWhere(
                                IBaseColumnsConstants._ID + " = " + Android_Content.ContentUris.ParseId(uri), where);
                        count = db.Update(MainTable.TABLE_NAME, values, constWhere, whereArgs);
                        break;

                    default:
                        throw new System.ArgumentException("Unknown URI " + uri);
                }

                GetContext().GetContentResolver().NotifyChange(uri, null);

                return count;
            }
All Usage Examples Of SQLiteDatabase::Update