Android SQLite: Update Statement

后端 未结 6 2030
半阙折子戏
半阙折子戏 2020-11-28 02:02

I need to implement SQLite in my application. I followed this tutorial.. Creating and using databases in Android one

Everything is working fine. I inserted 1 row wit

6条回答
  •  佛祖请我去吃肉
    2020-11-28 02:47

    The SQLiteDatabase object depends on the type of operation on the database.

    More information, visit the official website:

    https://developer.android.com/training/basics/data-storage/databases.html#UpdateDbRow

    It explains how to manipulate consultations on the SQLite database.

    INSERT ROW

    Gets the data repository in write mode

    SQLiteDatabase db = mDbHelper.getWritableDatabase();
    

    Create a new map of values, where column names are the keys

    ContentValues values = new ContentValues();
    values.put(FeedEntry.COLUMN_NAME_ENTRY_ID, id);
    values.put(FeedEntry.COLUMN_NAME_TITLE, title);
    values.put(FeedEntry.COLUMN_NAME_CONTENT, content);
    

    Insert the new row, returning the primary key value of the new row

    long newRowId;
    newRowId = db.insert(
         FeedEntry.TABLE_NAME,
         FeedEntry.COLUMN_NAME_NULLABLE,
         values);
    

    UPDATE ROW

    Define 'where' part of query.

    String selection = FeedEntry.COLUMN_NAME_ENTRY_ID + " LIKE ?";
    

    Specify arguments in placeholder order.

    String[] selectionArgs = { String.valueOf(rowId) };
    
    
    SQLiteDatabase db = mDbHelper.getReadableDatabase();
    

    New value for one column

    ContentValues values = new ContentValues();
    values.put(FeedEntry.COLUMN_NAME_TITLE, title);
    

    Which row to update, based on the ID

    String selection = FeedEntry.COLUMN_NAME_ENTRY_ID + " LIKE ?";
    String[] selectionArgs = { String.valueOf(rowId) };
        int count = db.update(
        FeedReaderDbHelper.FeedEntry.TABLE_NAME,
        values,
        selection,
        selectionArgs);
    

提交回复
热议问题