Android SQLite:删除语句准备查询出现问题
Android SQLite : Issue with delete statement prepared query
在我的 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:靠近“?”:语法错误(代码 1):,编译时:从 my_table 哪里删除? IN (SELECT ? FROM ? WHERE ? = '?' AND ? = 1 AND ? = 1 ORDER BY ? DESC LIMIT 1)
有什么帮助吗?
您只能使用 ?
来绑定文字,而不能使用 table 或列名等标识符。绑定文字时,?
本身不能被引号('?'
是包含 ?
的字符串文字,而不是占位符。)
如果你已经有一个正常工作的 execSQL()
原始 SQL,我真的不明白尝试使用 delete()
的意义。您也可以将 ?
占位符与 execSQL()
一起使用,例如 name
.
等字符串文字
在我的 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:靠近“?”:语法错误(代码 1):,编译时:从 my_table 哪里删除? IN (SELECT ? FROM ? WHERE ? = '?' AND ? = 1 AND ? = 1 ORDER BY ? DESC LIMIT 1)
有什么帮助吗?
您只能使用 ?
来绑定文字,而不能使用 table 或列名等标识符。绑定文字时,?
本身不能被引号('?'
是包含 ?
的字符串文字,而不是占位符。)
如果你已经有一个正常工作的 execSQL()
原始 SQL,我真的不明白尝试使用 delete()
的意义。您也可以将 ?
占位符与 execSQL()
一起使用,例如 name
.