Reputation: 26563
I'm not experienced with SQL queries too much so I'm having some trouble in finding the solution to my problem.
I have a list of Bookmarks, for example: Facebook, Developers Console, Stack Overflow... and I also have a list of their URLs: http://m.facebook.com, https://market.android.com...
I'm trying to let the user search these 2 lists and show any similar result, for example - if the user searched for "face" - I want him to get "Facebook". And if the user searched for "market" (for the market.android.. URL), he'll get "Developers Console".
This is my SQL WHERE clause:
String where = Browser.BookmarkColumns.TITLE + " LIKE ? OR "
+ Browser.BookmarkColumns.URL + " LIKE ?";
where = "(" + where + ") AND " + Browser.BookmarkColumns.BOOKMARK + " == 1";
It works fine but it only finds complete strings (for example: to find "Facebook", I must search for "Facebook" OR "facebook").
Would love some help with that!
Thanks, Lior
UPDATE:
Relevant code:
//search
String[] selArgs = new String[] { query };
String where = Browser.BookmarkColumns.TITLE + " LIKE '%" + query + "%' OR "
+ Browser.BookmarkColumns.URL + " LIKE '%" + query + "%'";
where = "(" + where + ") AND " + Browser.BookmarkColumns.BOOKMARK + " == 1";
cur = managedQuery(BOOKMARKS_URI, projection,
where, null, Browser.BookmarkColumns.VISITS + " DESC");
Logcat stacktrace:
10-23 00:09:59.765: ERROR/AndroidRuntime(15101): FATAL EXCEPTION: main
10-23 00:09:59.765: ERROR/AndroidRuntime(15101): java.lang.RuntimeException: Unable to start activity ComponentInfo{PACKAGE}: android.database.sqlite.SQLiteException: unrecognized token: "'%face%) AND bookmark == 1) ORDER BY visits DESC": , while compiling: SELECT _id, title, url, visits, favicon, thumbnail, touch_icon, _id AS _id FROM bookmarks WHERE ((title LIKE '%face%' OR url LIKE '%face%) AND bookmark == 1) ORDER BY visits DESC
10-23 00:09:59.765: ERROR/AndroidRuntime(15101): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1768)
10-23 00:09:59.765: ERROR/AndroidRuntime(15101): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1784)
10-23 00:09:59.765: ERROR/AndroidRuntime(15101): at android.app.ActivityThread.access$1500(ActivityThread.java:123)
10-23 00:09:59.765: ERROR/AndroidRuntime(15101): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:939)
10-23 00:09:59.765: ERROR/AndroidRuntime(15101): at android.os.Handler.dispatchMessage(Handler.java:99)
10-23 00:09:59.765: ERROR/AndroidRuntime(15101): at android.os.Looper.loop(Looper.java:130)
10-23 00:09:59.765: ERROR/AndroidRuntime(15101): at android.app.ActivityThread.main(ActivityThread.java:3835)
10-23 00:09:59.765: ERROR/AndroidRuntime(15101): at java.lang.reflect.Method.invokeNative(Native Method)
10-23 00:09:59.765: ERROR/AndroidRuntime(15101): at java.lang.reflect.Method.invoke(Method.java:507)
10-23 00:09:59.765: ERROR/AndroidRuntime(15101): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
10-23 00:09:59.765: ERROR/AndroidRuntime(15101): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
10-23 00:09:59.765: ERROR/AndroidRuntime(15101): at dalvik.system.NativeStart.main(Native Method)
10-23 00:09:59.765: ERROR/AndroidRuntime(15101): Caused by: android.database.sqlite.SQLiteException: unrecognized token: "'%face%) AND bookmark == 1) ORDER BY visits DESC": , while compiling: SELECT _id, title, url, visits, favicon, thumbnail, touch_icon, _id AS _id FROM bookmarks WHERE ((title LIKE '%face%' OR url LIKE '%face%) AND bookmark == 1) ORDER BY visits DESC
10-23 00:09:59.765: ERROR/AndroidRuntime(15101): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:158)
10-23 00:09:59.765: ERROR/AndroidRuntime(15101): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:114)
10-23 00:09:59.765: ERROR/AndroidRuntime(15101): at android.content.ContentProviderProxy.bulkQueryInternal(ContentProviderNative.java:330)
10-23 00:09:59.765: ERROR/AndroidRuntime(15101): at android.content.ContentProviderProxy.query(ContentProviderNative.java:366)
10-23 00:09:59.765: ERROR/AndroidRuntime(15101): at android.content.ContentResolver.query(ContentResolver.java:271)
10-23 00:09:59.765: ERROR/AndroidRuntime(15101): at android.app.Activity.managedQuery(Activity.java:1550)
10-23 00:09:59.765: ERROR/AndroidRuntime(15101): at PACKAGE.doMySearch(APPNAME.java:725)
10-23 00:09:59.765: ERROR/AndroidRuntime(15101): at PACKAGE.onCreate(APPNAME.java:150)
10-23 00:09:59.765: ERROR/AndroidRuntime(15101): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-23 00:09:59.765: ERROR/AndroidRuntime(15101): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1722)
Upvotes: 1
Views: 1278
Reputation: 5183
You can try this:
String where = Browser.BookmarkColumns.TITLE + " LIKE '%" + x +"%' OR "
+ Browser.BookmarkColumns.URL + " LIKE '%" + x +"%'";
where = "(" + where + ") AND " + Browser.BookmarkColumns.BOOKMARK + " == 1";
where x is the text that user has inserted, i.e. face.
Hope this helps!
Upvotes: 2
Reputation: 6545
You can use the SQL-keyword LIKE
for this. Encapsulate it with the wildward (%) and you're way to go.
sqlite> select * from testtable where name like '%face%';
facebook|42
Upvotes: 1