我需要从数据库中选择所有以字母开头的名字,但没有字母" x"。
对于现在存在于以下代码中的一些标题(它很慢但它有效):
String qstr = "SELECT SUM("+SQLhelper.COL_COUNT +") FROM "
+ SQLhelper.TABLE_MYDETAILS + " WHERE " +
SQLhelper.COL_DETAILID + " LIKE 'sh%'" +
" or " + SQLhelper.COL_DETAILID + " LIKE 'sw%'" +
" or " + SQLhelper.COL_DETAILID + " LIKE 'cty%'"+
" or " + SQLhelper.COL_DETAILID + " LIKE 'twn%'"+
" or " + SQLhelper.COL_DETAILID + " LIKE 'tlm%'"+
" or " + SQLhelper.COL_DETAILID + " LIKE 'sp%'"+
" or " + SQLhelper.COL_DETAILID + " LIKE 'rac%'"+
" or " + SQLhelper.COL_DETAILID + " LIKE 'frnd%'"+
" or " + SQLhelper.COL_DETAILID + " LIKE 'elf%'"+
" or " + SQLhelper.COL_DETAILID + " LIKE 'pm%'"+
" or " + SQLhelper.COL_DETAILID + " LIKE 'cas%'"+
" or " + SQLhelper.COL_DETAILID + " LIKE 'oct%'"+
" or " + SQLhelper.COL_DETAILID + " LIKE 'hp%'"+
" or " + SQLhelper.COL_DETAILID + " LIKE 'njo%'"+
" or " + SQLhelper.COL_DETAILID + " LIKE 'loc%'"+
" or " + SQLhelper.COL_DETAILID + " LIKE 'stu%'"+
" or " + SQLhelper.COL_DETAILID + " LIKE 'iaj%'"+
" or " + SQLhelper.COL_DETAILID + " LIKE 'tlr%'"+
" or " + SQLhelper.COL_DETAILID + " LIKE 'lor%'"+
" or " + SQLhelper.COL_DETAILID + " LIKE 'ovr%'"+
" or " + SQLhelper.COL_DETAILID + " LIKE 'tnt%'"+
" or " + SQLhelper.COL_DETAILID + " LIKE 'pi%'"+
" or " + SQLhelper.COL_DETAILID + " LIKE 'idea%'"+
" or " + SQLhelper.COL_DETAILID + " LIKE 'min%'"+
" or " + SQLhelper.COL_DETAILID + " LIKE 'trn%'";
Cursor allc = db.rawQuery(qstr, null);
allc.moveToFirst();
int cnt = allc.getInt(0);
if(cnt==0) {
Toast.makeText(this, getResources().getString( R.string.myminifigsnotfound ), Toast.LENGTH_LONG).show();
return;
}
allc.close();
当我添加其他名称时必须添加到查询字符串中。 我尝试使用正则表达式:
sql = "SELECT " + SQLhelper.COL_DETAILID + "," + SQLhelper.COL_COUNT
+ " FROM " + SQLhelper.TABLE_INSTRUCTIONS
+ " where " +SQLhelper.TABLE_INSTRUCTIONS_SETID + "="+setid
+ " and "+ SQLhelper.COL_DETAILID +" LIKE '[a-wyz]%'";
但它不起作用,它返回一个空查询。 怎么做?
答案 0 :(得分:3)
回答这个问题可能有点晚了,但是我把它放在那些可能会在以后面临同样问题的人身上。要在Android的Sqlite中使用REGEXP运算符,您可以使用库SqliteRE:
https://github.com/mobi-life/sqlitere
如果需要,它还支持加密数据的可选功能。
答案 1 :(得分:0)
据我所知,正则表达式(REGEXP
)是Android上SQLite不支持的少数几项内容之一。只有一些占位符适用于LIKE
,这些占位符为%
和_
。
您可以做的是,转而查询:
SELECT SUM("+SQLhelper.COL_COUNT +")
FROM SQLhelper.TABLE_MYDETAILS
WHERE INSTR ('sh|sw|...', SUBSTR(SQLhelper.COL_DETAILID, 1, 2)) > 0
OR INSTR ('cty|twn|...', SUBSTR(SQLhelper.COL_DETAILID, 1, 3)) > 0;