sqlite LIKE problem in android

后端 未结 4 1785
攒了一身酷
攒了一身酷 2020-12-05 23:20

Hello i\'ve spent almost 2 hours trying to figure out why the LIKE statement doesn\'t work and i only get this error: 03-03 11:31:01.770: ERROR/AndroidRuntime(11767):

相关标签:
4条回答
  • 2020-12-05 23:46

    I think you shouldn't use selArgs for LIKE such a way. You may try this:

    Cursor cursor = m_db.query(MY_TABLE, new String[] {"rowid","Word"},"Word"+" LIKE '"+name+"%'", null, null, null, null);
    

    EDIT:

    OK, if you want be safe from SQL injections, don't use above solution, use this:

    Cursor cursor = m_db.query(MY_TABLE, new String[] {"rowid","Word"},"Word LIKE '?'", new String[]{name+"%"}, null, null, null);
    
    0 讨论(0)
  • 2020-12-05 23:51

    The solution is actually very easy. Just include the % inside your selectionArgs.

    String []selectionArgs = {name + "%"});
    
    0 讨论(0)
  • 2020-12-05 23:52

    Another -- perhaps cleaner -- solution is to use the || operator as described here: Sqlite binding within string literal

    0 讨论(0)
  • 2020-12-06 00:00

    This is how I did:

    String []columns = {"_id", "name"};
    String []selectionArgs = {name+"%"};
    db.query(true,"mydb",columns,"name LIKE ?",selectionArgs,null,null,null);
    
    0 讨论(0)
提交回复
热议问题