Reputation: 2625
In my Android App, am trying to delete a row from SQLite. Sorry, am new to this & so may be asking something stupid. Earlier I was using this query for the deletion:
String queryToExecute = "DELETE FROM " + Constants.TABLE_STATS
+ " WHERE " + Constants.COL_ID + " IN " + "(SELECT "
+ Constants.COL_ID + " FROM " + Constants.TABLE_STATS
+ " WHERE " + Constants.COL_NAME + " = '" + name
+ "' AND " + Constants.COL_ID_ONE + " = " + idOne
+ " AND " + Constants.COL_DB_ID + " = " + dbId
+ " ORDER BY " + Constants.COL_DATE + " DESC" + " LIMIT 1)";
databaseConn.execSQL(queryToExecute);
But am trying to convert this to a parameterized query like this:
String whereClause =
"? IN " + "(SELECT ? FROM ? WHERE ? = '?' AND ? = " + idOne
+ " AND ? = " + dbId
+ " ORDER BY ? DESC" + " LIMIT 1)";
String[] whereArgs = {Constants.COL_ID, Constants.COL_ID, Constants.TABLE_STATS, Constants.COL_NAME, name, Constants.COL_ID_ONE, Constants.COL_DB_ID, Constants.COL_DATE};
int numOfRowsDeleted =
databaseConn.delete(Constants.TABLE_STATS, whereClause,
whereArgs);
But this is causing a crash saying
android.database.sqlite.SQLiteException: near "?": syntax error (code 1): , while compiling: DELETE FROM my_table WHERE ? IN (SELECT ? FROM ? WHERE ? = '?' AND ? = 1 AND ? = 1 ORDER BY ? DESC LIMIT 1)
Any help ?
Upvotes: 0
Views: 182
Reputation: 152817
You can use ?
only to bind literals, not identifiers such as table or column names. When binding literals, the ?
must not be quoted itself ('?'
is a string literal containing ?
, not a placeholder.)
I don't really see the point of attempting to use delete()
if you already have a functioning execSQL()
raw SQL. You can use ?
placeholders with execSQL()
, too, for example for the string literals such as name
.
Upvotes: 2