Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Single Quote In SQL With a LIKE StateMent

Tags:

java

android

sql

I am having problem querying single quote while using the sql LIKE statement

this is my SQL query for searching the MUSIC file in the SD CARD.

final Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
final String[] cursor_cols = {
   MediaStore.Audio.Media.TITLE
};
where = MediaStore.Audio.Media.TITLE + " like ('%"+SomeSongTitle+"%')";
cursor = getContentResolver().query(uri, cursor_cols, where, null, null);

SomeSongTitle is some arbitrary input text that the a user input.

My Question is why when SomeSongTitle contains a single Quote(for example SomeSongTitle=don't), it crashes.

And How to fix it?

thankz for reading and hope to hear some solution from you guys =D. hehe

like image 995
xiaowoo Avatar asked Nov 19 '25 08:11

xiaowoo


1 Answers

If you don't want to do String substitution you can use SQLiteDatabase.rawQuery to get your Cursor object. And then do something like:

String query = "select * from your_table_name where" + MediaStore.Audio.Media.TITLE + " like ('%?%')";
cursor = yourDB.rawQuery(query, new String[] {SomeSongTitle});

That should get around the quoting issue.

like image 144
Marc Bernstein Avatar answered Nov 21 '25 22:11

Marc Bernstein



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!