简体   繁体   中英

how to search sqlite database using regular expression in android?

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?

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

正如James建议的那样,使用LIKE运算符,然后使用正则表达式对结果进行java搜索

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM