1

Tried everything. This UPDATE don't work. No error or something - it just not updates the table.

   void SetDisable (Integer ID, boolean action){
       Integer s;
       if (action==true) s = 0; else s = 1;
       db.rawQuery("UPDATE sms SET disabled="+s+" WHERE id="+ID+"",null);  
   }

This is my table schema:

CREATE TABLE sms (id INTEGER PRIMARY KEY, fromhour INT, frommin INT, tohour INT, tomin INT, disabled INT);
sqlite>
Dikobraz
  • 649
  • 2
  • 8
  • 22

3 Answers3

11

Try this:

 db.execSQL("UPDATE sms SET disabled="+s+" WHERE id="+ID+"");  

SQL queries which return a table (a cursor) are to be run with rawQuery, and those that do not return tables are to be run with execSQL

See this:

Increase the value of a record in android/sqlite database

Community
  • 1
  • 1
Sourab Sharma
  • 2,940
  • 1
  • 25
  • 38
  • You can use new Object[]{} (empty array of Objects) as your bind args if you don't have anything to replace in the query. Not sure if that will fully solve the problem. There is a different form of execSQL that takes args if you need those. – parkerfath Jul 31 '12 at 21:19
  • 1
    This is the correct answer, and it saved me many more hours to waste. I was using rawQuery() on my UPDATE. I also didn't want to trigger ContentObserver, so couldn't use the ContentProvider either. My table is pretty big, and I wanted to UPDATE all the rows first before calling notifyChange(), whereas ContentObserver calls notifyChange() on every single row update, which is a pretty bad design in my case. – zeeshan Apr 15 '15 at 19:53
  • It's awful, that SQLite does not fire any warning in such cases and update is just suppressed silently. – Stan Feb 05 '16 at 13:45
5

It's easy to use the update method provided by the SQLiteDatabase class.

void SetDisable (Integer ID, boolean action){
    Integer s;
    if (action==true) s = 0; else s = 1;

    ContentValues cv=new ContentValues();
    cv.put(disabled, s);

    String where = "id=?";
    String[] whereArgs = {Integer.toString(ID)};

    db.update(sms, cv, where , whereArgs);   
}
J. Maes
  • 6,862
  • 5
  • 27
  • 33
  • yes it is easy to use update method for SQLiteDataBase but in case if you want to update the whole record. If you want to update only single or just a couple of fields, the best way is to use simple execSQL method. If you try to update single field with rawQuery it wouldn't update, that's why just use execSQL(). – Stoycho Andreev Dec 05 '15 at 18:01
-1

Updated:

Try this

ContentValues cv = new ContentValues();
cv.put("disabled", s);

db.update("table_name", cv, "id="+ID, null);

NOTE: I don't know if it is enough to put ONLY what you need to change in cv or you HAVE TO put all the attributes.

iTurki
  • 16,292
  • 20
  • 87
  • 132