user1482641
user1482641

Reputation: 103

Special characters in LIKE with SQLite

I want to search in DB for like $!(%&'^%$&'%^&&'^'%'

I tried following queries but still error is shown:

select * from data
where data1 LIKE \'$!(%&'^%$&'%^&&'^'%'\'

select * from data
where data1 LIKE '$!(%&'^%$&'%^&&'^'%''

select * from data
where data1 LIKE '$!(%&\'^%$&\'%^&&\'^\'%\''

select * from data
where data1 LIKE '$!(%&\\'^%$&\\'%^&&\\'^\\'%\\''

select * from data
where data1 LIKE '$!(%&\'^%$&\'%^&&\'^\'%\'' ESCAPE '\'

Upvotes: 0

Views: 1300

Answers (2)

user1482641
user1482641

Reputation: 103

Thank you all.
I got the answer.

String originalString = "$!(%&'^%$&'%^&&'^'%'";
String convertedString = DatabaseUtils.sqlEscapeString(num);

Pass this convertedString as parameter to query.

Upvotes: 1

Sam
Sam

Reputation: 86948

From SQLite's documentation:

A single quote within the string can be encoded by putting two single quotes in a row

So double up your single quotes, like this:

select * from data
where data1 LIKE "$!(%&''^%$&''%^&&''^''%''"

But honestly, I had to do a little more reading to see if I missed any other special characters...

Upvotes: 0

Related Questions