I have a db that is structred as such
uid | username | password | email | cur_level
1 default abc123 me@email.com 0
In my game after you complete a level in the main activity I run a method called increase_current_level()
value++;
test_db.open();
test_db.updateLevel(value);
cur_level = value;
test_db.close();
So that seems to work with no problems but when test_db.updateLevel(value) is run, that when the program crashes.
In my DBHelper the code for update level is as follows.
public boolean updateLevel(String level) {
ContentValues args = new ContentValues();
args.put(KEY_CUR_LEVEL, level);
return mDb.update(DATABASE_TABLE, args, KEY_ROWID + "="
+ "WHERE id=(SELECT max(id) FROM DATABASE_TABLE)", null) > 0;
}
My log cat error seems to be:
10-17 17:36:10.833: E/AndroidRuntime(1815): Caused by: android.database.sqlite.SQLiteException: near "WHERE": syntax error (code 1): , while compiling: UPDATE test SET cur_level=? WHERE _id=WHERE id=(SELECT max(id) FROM DATABASE_TABLE)
I’m very new to SQLite so, it’s kind of intimidating, but I hope someone sees a very obvious flaw. Thanks
Update 1:
My updated code:
public boolean updateLevel(String level) {
ContentValues args = new ContentValues();
args.put(KEY_CUR_LEVEL, level);
return mDb.update(DATABASE_TABLE, args, KEY_ROWID + " = (SELECT max(id) FROM DATABASE_TABLE",
null) > 0;
}
Updated error msg:
10-17 18:26:50.804: E/AndroidRuntime(3684): Caused by: android.database.sqlite.SQLiteException: near "DATABASE_TABLE": syntax error (code 1): , while compiling: UPDATE test SET cur_level=? WHERE _id = (SELECT max(id) FROM DATABASE_TABLE
The syntax of
mDb.updateisLet’s assume that
KEY_ROWID = "id"For your
whereClause, you’re literally passingwhich is not valid SQL.
You don’t seem to need “
id =“. Just start yourWHEREclause with “WHERE id=“However, your question implies that you have only one record in the DB. Why use a DB for one record? And if you have more than one record in it, you should use a
WHEREclause that searches on something unique, rather than usingSELECT max(id).The bottom line is that you need to improve your knowledge of SQL.