我正在尝试从SQLite数据库中获取表名列表。我设法得到这个,但我也得到临时表名称,如'android_metadata'和'sqlite_sequence'。我想排除这两个。
但是,我无法让它正常工作。
SQLiteDatabase database =
getBaseContext().openOrCreateDatabase("vocabularyDatabase",
MODE_PRIVATE, null);
Cursor c = database.rawQuery( "SELECT name FROM sqlite_master
WHERE (type = 'table') AND (name NOT LIKE 'sqlite_sequence' OR
name NOT LIKE 'android_metadata') ",
null);
if (c.moveToFirst()){
while (!c.isAfterLast() ){
listOfWords.add(c.getString(c.getColumnIndex("name"))
);
c.moveToNext();
}
}
答案 0 :(得分:1)
假设当前行是android_metadata
。表达式name NOT LIKE 'android_metadata'
将为false,但表达式name NOT LIKE 'sqlite_sequence'
将为true。所以WHERE子句减少到true AND (true OR false)
,这是真的。
您需要将OR
替换为AND
:
WHERE type = 'table'
AND (name NOT LIKE 'sqlite_sequence' AND
name NOT LIKE 'android_metadata')
如果你真的想使用OR
,你必须申请DeMorgan's laws并否定整个条件:
WHERE type = 'table'
AND NOT (name LIKE 'sqlite_sequence' OR
name LIKE 'android_metadata')
或者,只需将IN与列表一起使用:
WHERE type = 'table'
AND name NOT IN ('sqlite_sequence', 'android_metadata')