sqlite db update

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 search by the primary key and then update a row in the table.


You can use rawQuery like this:

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


  • cur is Cursor object
  • _id is name of the column (only example)

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

Read the documentation for SQLiteDatabase.update

You should end up with something like this:

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


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.

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

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

The answer is:




Try this:

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

Hope it will help.

Using database.update make it simple like this:

ContentValues values = new ContentValues();
    values.put(MySQLiteHelper.COLUMN_NAME, name);
    values.put(MySQLiteHelper.COLUMN_JOB, job);
    values.put(MySQLiteHelper.COLUMN_DATE_START, date_start);
    database.update(MySQLiteHelper.TABLE_EMPLOYEES, values, MySQLiteHelper.COLUMN_ID+"="+id, null);

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,
            NotesDbContract.NoteEntry._ID + "=?", new String[]{String.valueOf(note.getId())}

    return result > 0;

Need Your Help

How to specify newsstand icon for iOS 7 in .plist?

ios7 icons plist newsstand-kit

Hello everyone I am trying to specify newsstand icon for iOS 7.0. Where should i specify it in the app .plist file. I have Icon Files ( iOS 5) set for the newsstand shelf icon, and its working perf...

How do I access DistributedCache in Hadoop Map/Reduce jobs?

hadoop mapreduce distributed-cache

I'm trying to pass a small file to a job I'm running using the GenericOptionsParser's -files flag: