如果字符串包含双引号("
)字符,如何在SQLite中搜索?
当我尝试搜索这些应用程序崩溃时。
我的代码
public Boolean readbookname(String bookname) {
bookname=bookname.replace("\"", "\"'");
Cursor cursor = myDataBase.rawQuery("select book_name From book_data WHERE book_name=\"" + bookname + "\"", null);
if (cursor.moveToFirst()) {
return true;
} else {
return false;
}
}
错误日志
android.database.sqlite.SQLiteException: unrecognized token: "'"" (code 1): , while compiling: select book_name From book_data WHERE book_name="'"'"
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:893)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:504)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1341)
at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1280)
at ktech4.brilliantinnovator.com.k4tech.DBManager.readbookname(DBManager.java:355)
at ktech4.brilliantinnovator.com.k4tech.CustomDialogClass.onClick(CustomDialogClass.java:74)
at android.view.View.performClick(View.java:4803)
at android.view.View$PerformClick.run(View.java:20102)
at android.os.Handler.handleCallback(Handler.java:810)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:189)
at android.app.ActivityThread.main(ActivityThread.java:5532)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
答案 0 :(得分:1)
删除bookname=bookname.replace("\"", "\"'");
并更改
Cursor cursor = myDataBase.rawQuery("select book_name From book_data WHERE book_name=\"" + bookname + "\"", null);
要
Cursor cursor = myDataBase.rawQuery("select book_name From book_data WHERE book_name='" + bookname + "'", null);
答案 1 :(得分:0)
"select book_name From book_data WHERE book_name='" + bookname + "'"
尝试将您的查询设为这样