问题
The song name that I want to insert it into my table contains a " ' " (apostrophe )so , there is an error when the request is excuted. How can I fix it
Cursor pid = mDB.rawQuery("select Id FROM MusicPlayer WHERE "Path ='" + sname + "';", null);
I am getting sname runtime so .. sname=/mnt/sdcard/Now Thats What I Call Music 85 [Bubanee]/16 - Let's Get Ready To Rhumble (100% Radio Mix) - PJ & Duncan.mp3
I get below error..
android.database.sqlite.SQLiteException: near "s": syntax error: , while compiling: SELECT Id FROM MusicPlayer WHERE Path ='/mnt/sdcard/Now Thats What I Call Music 85 [Bubanee]/16 - Let's Get Ready To Rhumble (100% Radio Mix) - PJ & Duncan.mp3';
Because sname contain let's
word with '
which gives me error.
回答1:
In theory you could escape '
as ''
in SQL.
However, it's better to use variable binding. Replace the string literals in SQL with ?
and supply corresponding number of String
s in an array:
Cursor pid = mDB.rawQuery("select Id FROM MusicPlayer WHERE Path = ?;",
new String[] { sname });
回答2:
you can get details for Sting within comma or someother means use like this cursor.
Cursor cursor = odb.rawQuery("SELECT * FROM " + DATABASE_TABLE + " WHERE " + KEY_ITEM + "=?;", new String[]{comboname});
Thanks laalto sir your answer is work perfectly.
来源:https://stackoverflow.com/questions/22126209/sqlite-sqliteexception-near-s-syntax-error-because-of-apostrophe