tags:

views:

135

answers:

1

I try to increase the value of an integer key in a row in my table. However, nothing really seems to happen.

db.rawQuery("UPDATE table SET key = key + 1 WHERE name=?", new String[] {name});

However, this code works fine (just sets the key to a hard-coded value):

    ContentValues values = new ContentValues();
    values.put("key", 2);
    db.update("table", values, "name=?", new String[] {name});

Also tried '?' instead of just ?, but it resulted just in a run-time error.

A: 

From http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html:

public Cursor rawQuery (String sql, String[] selectionArgs)

Runs the provided SQL and returns a Cursor over the result set.

Returns A Cursor object, which is positioned before the first entry

compared to:

public void execSQL (String sql, Object[] bindArgs)

Execute a single SQL statement that is not a query. For example, CREATE TABLE, DELETE, INSERT, etc.

In other words, SQL queries which return a table are to be run with rawQuery, and those that do not return tables are to be run with execSQL

MPelletier
I still don't really get it. If rawQuery really "runs the provided SQL" as stated in the docs, I can't understand why it wouldn't work for any kind of SQL statements. Of course, that's just a theoretical question, but still a bit strange.
nico
It's still a good question. The answer must lie with the where clause. I don't know how android treats this function, but if one function quotes the string argument, and if the other does not, that could explain why the number is not increased. Still, in example code I have seen, the `db.update` appears to be popular. More so than `db.execSQL("UPDATE...`
MPelletier