Ganesh Gudghe Ganesh Gudghe - 5 months ago 53
Android Question

search from sqlite table string contais double quotes in android

how to search from Sqligh if string contain Double Quotes character.

when i am trying to search like these my app crashed.

following is my code

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;
}
}


Error log

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)


Can someone help me to how can i do that

Answer

Remove bookname=bookname.replace("\"", "\"'"); and Change

 Cursor cursor = myDataBase.rawQuery("select book_name From book_data WHERE book_name=\"" + bookname + "\"", null);

To

 Cursor cursor = myDataBase.rawQuery("select book_name From book_data WHERE book_name='" + bookname + "'", null);
Comments