sqlite.SQLiteException: near “s”: syntax error: because of apostrophe

杀马特。学长 韩版系。学妹 提交于 2021-02-19 07:14:26

问题


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 Strings 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!