5

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.

8 Answers 8

13

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'");
Sign up to request clarification or add additional context in comments.

Comments

6

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.

1 Comment

plus one for the Update. Always prefer the db.update() method instead of the manual raw query. Trying to store a json that contains an apostrophe will convince you too
3

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)

2 Comments

Thanks I used this. :) Thanks a lot for the help mate! :)
Avoid using error-prone syntax by using string.format to build your query i.e String.format("update %s set column1=%s where id='%s'",TABLE_NAME,"mango",_id) @Waqas answer is more correct as it allows for a variable number of fields to be updated without altering the code.
1

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

Comments

0

The answer is:

http://www.sqlite.org/lang_update.html

and

SQLiteDatabase.rawQuery(...)

Comments

0

Try this:

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

Hope it will help.

1 Comment

Consider using string.format instead of + for strings ... it's much more legible and less error-prone
0

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);

Comments

0

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;
}

2 Comments

Thanks, but what is another way and why db.close();?
@CoolMind I recommend using the result to get a return flag about our delete command success, so we can decide how to navigate the code flow based on it, for ex refresh a view to hold the new values.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.