0

I am trying to search my notes whose title start with "n" but i dont know whats wrong with my query i got force close error.
Here is my log:

04-01 12:19:37.833: D/AndroidRuntime(676): Shutting down VM  
04-01 12:19:37.833: W/dalvikvm(676): threadid=1: thread exiting with uncaught exception (group=0x40015560)  
04-01 12:19:37.913: E/AndroidRuntime(676): FATAL EXCEPTION: main  
04-01 12:19:37.913: E/AndroidRuntime(676): android.database.sqlite.SQLiteException: near "%": syntax error: , while compiling: SELECT DISTINCT _id, title, body, cdate, passw FROM notes WHERE title LIKE n%  
04-01 12:19:37.913: E/AndroidRuntime(676):  at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
04-01 12:19:37.913: E/AndroidRuntime(676):  at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:92) 

Here is the code:

public Cursor search(String title) {
    Cursor mCursor =
            mDb.query(true, DATABASE_TABLE, new String[] {KEY_ROWID,
                    KEY_TITLE, KEY_BODY,KEY_CDATE,KEY_PASS}, KEY_TITLE + " LIKE" +" " + title +  '%', null,
                    null, null, null, null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;
 }
aizaz
  • 3,056
  • 9
  • 25
  • 57
Razin
  • 181
  • 1
  • 13

3 Answers3

1

You need literal between text

public Cursor search(String title) {
    Cursor mCursor =
            mDb.query(true, DATABASE_TABLE, new String[] {KEY_ROWID,
                    KEY_TITLE, KEY_BODY,KEY_CDATE,KEY_PASS}, KEY_TITLE + " LIKE '" + title +  "%'", null,
                    null, null, null, null);

            mCursor.moveToFirst();

        return mCursor;
 }
bsiamionau
  • 8,099
  • 4
  • 46
  • 73
Hoan Nguyen
  • 18,033
  • 3
  • 50
  • 54
1

check this two links: one and two

or just change this:

query(true, DATABASE_TABLE, new String[] {KEY_ROWID,
                KEY_TITLE, KEY_BODY,KEY_CDATE,KEY_PASS}, KEY_TITLE + " LIKE "+"'" + title +  "%'", null,
                null, null, null, null);
Community
  • 1
  • 1
Dhaval Parmar
  • 18,812
  • 8
  • 82
  • 177
0

The query should be something like:

select * from 'product' where product_name like 'crocin%'

once you add ' (single quote) in the query, it should be working.

Pablo Romeo
  • 11,298
  • 2
  • 30
  • 58