Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am trying to search a database using regular expression in sqlite. Here is my code:

cursor = db.rawQuery("SELECT _id, employee FROM employees WHERE employee REGEXP ?", 
                    new String[]{"%" + prepareSearchText()  + "%"});
    adapter = new SimpleCursorAdapter(
            this,
            R.layout.search_row, 
            cursor, 
            new String[] {"_id", "employee "}, 
            new int[] {R.id.idField, R.id.nameField});
    setListAdapter(adapter);

However I am getting this error:

Caused by: android.database.sqlite.SQLiteException: no such function: REGEXP.

I used the function REGEXP in MySQL before so I assumed it exists in sqlite, but apparently it is not.

What is the best way to perform regular expression when querying sqlite database in android?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
665 views
Welcome To Ask or Share your Answers For Others

1 Answer

In sqllite the "REGEXP" function is not included by default and you need to "roll your own".

However the "LIKE" operator should do what you want as long as your searches are reasonably simple.

the docs


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...