gpt4 book ai didi

java - 如果字符串包含双引号,则搜索 SQLite 表

转载 作者:行者123 更新时间:2023-12-01 09:46:02 25 4
gpt4 key购买 nike

如果字符串包含双引号(")字符,如何在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)

最佳答案

删除 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);

关于java - 如果字符串包含双引号,则搜索 SQLite 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38028164/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com