instr() function SQLITE for Android?

前端 未结 5 984
误落风尘
误落风尘 2020-12-11 23:59

I wanna do the following on my existing sqlite database on android which is kind a built like that colomns: id --- rule --- path --- someotherdata

A rule now e.g. co

相关标签:
5条回答
  • 2020-12-12 00:13

    Use underscore(_) in the LIKE pattern instead of percentage(%). Underscore will matches any single character in the value instead of the entire string sequence.

    "SELECT DISTINCT * FROM " + TABLE_RULES + " WHERE rule LIKE '_"+ value + "_' ORDER BY " + KEY_ID+ " DESC;"
    

    LIKE operators

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

    I can't see anything wrong with your SQL expression ... the only thing that might be killing it is the lack of spaces around the inequality operator !=

    Try this:

    "SELECT DISTINCT * FROM " + TABLE_RULES + " WHERE instr('"+ value + "',rule) > 0 ORDER BY " + KEY_ID+ " DESC;"
    

    I'm not sure if the SQLite implementation in Android is complete, you may find that some ops don't work, so do some simple tests as a sanity check.

    If that doesn't work you can reverse the condition.

    [field]='some_value'
    

    is just as valid as:

    'some_value'=[field]
    

    So in your example I thought you should be able to do something like:

    select distinct * from TABLE_RULES
    where 'poniesAsUnicorns.jpg' like '%' + rule + '%';
    

    I tested this in mySQL and it doesn't like the + concatenation but it does work happily like this:

    select distinct * from TABLE_RULES
    where 'poniesAsUnicorns.jpg' like replace('%rule%','rule',rule);
    
    0 讨论(0)
  • 2020-12-12 00:22
    "SELECT DISTINCT * FROM " + TABLE_RULES + " WHERE rule LIKE '%"+ value + "%' ORDER BY " + KEY_ID+ " DESC;"
    

    try this one.

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

    AFAIK instr() is not available, so you can use:

    select * from table where replace("poniesAsUnicorns.jpg", rule, "") != "poniesAsUnicorns.jpg"; 
    

    or for case insensitive matches:

    select * from table where replace(upper("poniesAsUnicorns.jpg"), upper(rule), "") != upper("poniesAsUnicorns.jpg");
    
    0 讨论(0)
  • 2020-12-12 00:35
    "SELECT DISTINCT * FROM " + TABLE_RULES + " WHERE '" + value + "' like '%' || rule || '%' ORDER BY " + KEY_ID + " DESC;"
    
    0 讨论(0)
提交回复
热议问题