11

I have seen several questions here related to updating in sqlite, but not able to solve this problem. I am unable to update table, it always give 0;

public void onCreate(SQLiteDatabase db) {
    try{
    db.execSQL(createTable);
    db.execSQL(createQuestionTable);
    populateQuestionTable(db);
    }

    catch( SQLException e)
    {
        e.printStackTrace();
    }
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS alarms" );
    onCreate(db);

}

I am able to insert and fetch data, but there is only problem in updating it.

public int updateTable( int r )
{
    String row = String.valueOf(r);
    Log.d("mydb", "disbale alarm" + " " + row);
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(isDisable, 1);

    int result = 0;
    try{
     result = db.update("alarms", values,  keyId+" = ? " ,new String[]{row});
    Log.d("result", ""+result);
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }

    db.close();

    return result;
}

P.S. I have also tried execSql()

Haris
  • 267
  • 1
  • 2
  • 13

3 Answers3

13

It sounds like nothing matches your criteria. Perhaps as a test you should pass null as your where criteria (this signifies update all rows) and see if 0 is still returned.

If 0 is still returned after that I would look to ensure you actually have data saved in your database table.

  • 1
    I have passed null, and got 1 in result, So thats mean its problem in where clause? – Haris Feb 26 '14 at 19:27
  • I am using sqlite manager to see database, but values I am passing are matching criteria. – Haris Feb 26 '14 at 19:31
  • Yes that means it is a problem in the where clause or the new String[]{row} code. Make sure there is no typo in the column name and that the value exists in your database. Is the column in your database initialized as type of integer? –  Feb 26 '14 at 19:34
  • 1
    I can't believe it, I just forgot database id starts with 1 not with 0. Thanks a ton man for this debugging. – Haris Feb 26 '14 at 19:36
8

SQLiteDatabase.update() returns number of rows affected. So, in your case, no rows were affected (most probably because of your WHERE clause)

n1k1ch
  • 2,594
  • 3
  • 31
  • 35
0

Don't forget to start with id = 1 and not 0 when you use datebase. i had the same probleme update and delete didn't work because i had the wrong starting id.

XCarb
  • 735
  • 10
  • 31