vendredi 23 janvier 2015

Android SQLite : Issue with delete statement prepared query

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 ?


Aucun commentaire:

Enregistrer un commentaire