Trying to execute LIKE clause in sqlite3 in iPhone
-
20-12-2019 - |
Question
I am trying to execute LIKE clause in my sqlite3 query i.e.
sqlStatement = [NSString stringWithFormate;@"select * from table_name where column_name like '%"+%@+"%'",title];
Here title is a NSString type. having values like 6 Kt or 10 Kt or 14 Kt or 18 Kt
And in column_name i have values like this (6 Kt, 10 Kt, 14 Kt) in single statement
|------------------------|
| column_name |
|------------------------|
| 6 Kt, 10 Kt, 14 Kt |
| -------------------- |
| 10 Kt, 18 Kt, 22 Kt |
| --------------------- |
| 6 Kt, 10 Kt, 24 Kt |
| --------------------- |
| 6 Kt, 14 Kt, 22 Kt |
| ---------------------- |
| 6 Kt, 22 Kt, 24 Kt |
| -----------------------|
and i try one more query like this :
sqlStatement = [NSString stringWithFormat:@"select * from jewelryInfo where karat like '%@%%'",title];
with the value of title = 6 Kt.
IT give the values of a row starting with 6 Kt
but when i try to use 18 Kt or 22 Kt as a value for title
it doesn't give me any result
I think it is only checking the first value of a row in this column.
please help me, Thanks
Solution
You must add a %
before query. Also, you must use following query:
sqlStatement = [NSString stringWithFormat:@"select * from jewelryInfo where ', '||karat like '%% %@%%'",title];
to match items in middle, beginning or end of list and match 6 but not 16.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow