-1

In my app, when the activity is destroyed, I want to update a column value to 0. For that I have written the query, but it seems not to be working for me, because when ever I start the app I get the same old values.

Code

public void resetSelectOptions() {

    database = DatabaseManager.getInstance().openDatabase();
    String query = "Update " + TableName + " SET " + Selected_Option + "=0";

    try {
        database.rawQuery(query, null);
    }
    catch (SQLiteException e) {

    }
    DatabaseManager.getInstance().closeDatabase();
}

How can I fix this problem?

Community
  • 1
  • 1
Anuj Mody
  • 21
  • 1
  • 6

3 Answers3

0

rawQuery() only executes queries (SELECT).
For any other SQL command, use execSQL().

So, simply convert this

database.rawQuery(query, null);

to this

database.execSQL(query, null);
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
0

Use this method to updating the rows,database.update() and rawQuery is used to select query.

Krishna V
  • 1,801
  • 1
  • 14
  • 16
-1

It is not correct that the rawQuery() method executes only SELECT queries, it doesn't, but it is unnecessary to use it for an update, because you don't have any result - what to be iterated with the result Cursor at the ends, so try to use the execSQL() method. For more information take a look at this SO question android.database.sqlite.SQLiteDatabase.rawQuery() is not updating a DATETIME column with a SQLite datetime() function

Community
  • 1
  • 1
Gabriella Angelova
  • 2,985
  • 1
  • 17
  • 30
  • Please, learn about `rawQuery()`: http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#rawQuery(java.lang.String,%20java.lang.String[],%20android.os.CancellationSignal)) and `execSQL()`: http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#execSQL(java.lang.String) – Phantômaxx May 23 '15 at 15:01