我想知道如何绑定SQLite FTS
查询参数以清理输入。我试图运行如下所示的原始查询,最终导致运行时错误。
当我创建表格时。效果很好,
private const val SQL_CREATE_ENTRIES =
"CREATE VIRTUAL TABLE entry USING FTS4 (title, subtitle);"
db.execSQL(SQL_CREATE_ENTRIES)
某些条目已成功插入到数据库,
val values = ContentValues().apply {
put("title", "Test title")
put("subtitle", "Test subtitle")
}
val newRowId = db?.insert("entry", null, values)
现在我正在运行原始查询,
val query = "SELECT * FROM entry WHERE title MATCH '?'"
val args = arrayOf("Test")
val cursor: Cursor? = db.rawQuery(query, args)
此操作因运行时错误而失败,
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.achellies.kotlin/com.achellies.kotlin.MainActivity}: java.lang.IllegalArgumentException: Cannot bind argument at index 1 because the index is out of range. The statement has 0 parameters.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3086)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3229)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1926)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:6981)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1445)
Caused by: java.lang.IllegalArgumentException: Cannot bind argument at index 1 because the index is out of range. The statement has 0 parameters.
at android.database.sqlite.SQLiteProgram.bind(SQLiteProgram.java:240)
at android.database.sqlite.SQLiteProgram.bindString(SQLiteProgram.java:194)
对于non FTS
数据库,我们可以使用=?
绑定进行查询。因此,如何对具有FTS4 SQLite
子句的MATCH
查询进行清理?
答案 0 :(得分:0)
val query = "SELECT * FROM entry WHERE title MATCH '?'"
'?'
应该写成单引号。正确的方法是
val query = "SELECT * FROM entry WHERE title MATCH ?"