Sqlite db update

Is there an easy way to update a table in sqlite in android? (e.g. one line of an inline method)? I have a table with multiple columns, and primary is a single column. I want to search by primary key and then update the row in the table.

+4
source share
8 answers

You can use rawQuery as follows:

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

Where

  • cur - Cursor object
  • TABLE_NAME NAME OF THE TABLE
  • _id name of the column (example only)
+3
source

To use the method from android with the predefined update , use it as shown 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 to this (not recommended):

 db.execSQL("UPDATE table_name SET col_name='new_value' WHERE primary_column='primary_id'"); 
+12
source

Read the documentation for SQLiteDatabase.update

You should get something like this:

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

UDPATE

Avoid raw queries using syntax prone to errors at all costs. I see a lot of answers here that use a lot of '"' + SOMETHING + "'" ... this is a very bad practice and you will spend all your time looking for errors in hard-to-reach places or just a waste of time.

If you must use raw queries, try forming them using String.format to avoid dangerous debugging and migraine sessions.

+5
source

Then you should already know what your primary key is.

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

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

+1
source

Try the following:

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

Hope this helps.

0
source

Using database.update , do the following:

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

I know this is a bit old, but in case someone needs a different 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
source

All Articles