sqlite db update

后端 未结 8 1671
暗喜
暗喜 2020-12-11 09:14

Is there an easy way to update a table in sqlite in android? (like a single line in built method) ? I have a table with few columns and primary is one column. I want to sear

相关标签:
8条回答
  • 2020-12-11 09:15

    To use with predefined update method from android, use it as below:

    ContentValues args = new ContentValues();
    args.put("col_name", "new value");
    
    db.update("table_name", args, String.format("%s = ?", "primary_column"),
               new String[]{"primary_id"});
    

    Or to run as a single line, go with this (not recommended):

    db.execSQL("UPDATE table_name SET col_name='new_value' WHERE
               primary_column='primary_id'");
    
    0 讨论(0)
  • 2020-12-11 09:16

    Then you should already know what's your primary key.

    dbHelper.getWritableDatabase();
    ContentValues values = createContentValues(profileVo);
    db.update(ProfileVO.TABLE_NAME, values, ProfileVO.COLUMN_ID + "=" + profile.getId(), null)
    

    Here's a good tutorial for you http://www.vogella.com/articles/AndroidSQLite/article.html

    0 讨论(0)
  • 2020-12-11 09:16

    Try this:

    public void updateFunction(int id) {
                String updateStmnt  = "UPDATE  YOUR_TABLE SET YOUR_COLUMN = "
                        + id;
                database.execSQL(updateStmnt);
            }
    

    Hope it will help.

    0 讨论(0)
  • 2020-12-11 09:27

    I know this a bit old, but in case anyone needed another way:

    public boolean updateNote(Note note) {
        SQLiteDatabase db = notesDbHelper.getWritableDatabase();
    
        ContentValues contentValues = new ContentValues();
        contentValues.put(NotesDbContract.NoteEntry._ID, note.getId());
        contentValues.put(NotesDbContract.NoteEntry.COLUMN_NAME_TITLE, note.getTitle());
        contentValues.put(NotesDbContract.NoteEntry.COLUMN_NAME_DSECRIPTION, note.getDescription());
    
        int result = db.update(NotesDbContract.NoteEntry.TABLE_NAME,
                contentValues,
                NotesDbContract.NoteEntry._ID + "=?", new String[]{String.valueOf(note.getId())}
        );
        db.close();
    
        return result > 0;
    }
    
    0 讨论(0)
  • 2020-12-11 09:32

    You can use rawQuery like this:

    cur = mDb.rawQuery("update " + TABLE_NAME
    + " set column1=mango where id='" + _id + "'",null);
    

    where

    • cur is Cursor object
    • TABLE_NAME is NAME OF THE TABLE
    • _id is name of the column (only example)
    0 讨论(0)
  • 2020-12-11 09:32

    Read the documentation for SQLiteDatabase.update

    You should end up with something like this:

    affected = db.update(TABLE_NAME, values, where, whereArgs);

    UDPATE

    Avoid raw queries using error-prone syntax at all costs. I see a lot of answers here that use a lot of '"' + SOMETHING + "'" ... this is extremely bad practice and you will spend all your time looking for errors on places that are hard to find or simply a complete waste of time.

    If you must use raw queries, try forming them with String.format to avoid perilous debug sessions and migraines.

    0 讨论(0)
提交回复
热议问题