Question

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.

Was it helpful?

Solution

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

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top