在我的Android应用中,我试图从SQLite中删除一行。对不起,我是这个&所以可能会问一些愚蠢的事情。 之前我使用此查询进行删除:
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);
但我正在尝试将此转换为参数化查询,如下所示:
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);
但是这导致了崩溃说
android.database.sqlite.SQLiteException:near"?&#34 ;:语法错误(代码1):,同时编译:DELETE FROM my_table WHERE? IN(SELECT?FROM?WHERE?='?' AND?= 1 AND?= 1 ORDER BY?DESC LIMIT 1)
任何帮助?
答案 0 :(得分:2)
您只能使用?
来绑定文字,而不是表格或列名称等标识符。绑定文字时,?
不得自行引用('?'
是包含?
的字符串文字,而不是占位符。)
如果您已经有一个正常运行的delete()
原始SQL,我真的没有意识到尝试使用execSQL()
的意义。您也可以将?
占位符与execSQL()
一起使用,例如name
等字符串文字。