问题
I am trying to get a list of table names from an SQLite database. I manage to get this, but I also get temporary table names like 'android_metadata' and 'sqlite_sequence'. I want to exclude these 2.
However, I can't get it to work right.
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();
}
}
回答1:
Assume that the current row is for android_metadata
. The expression name NOT LIKE 'android_metadata'
will be false, but the expression name NOT LIKE 'sqlite_sequence'
will be true. So the WHERE clause reduces to true AND (true OR false)
, which is true.
You need to replace the OR
with AND
:
WHERE type = 'table'
AND (name NOT LIKE 'sqlite_sequence' AND
name NOT LIKE 'android_metadata')
If you really want to use OR
, you have to apply DeMorgan's laws and negate the entire condition:
WHERE type = 'table'
AND NOT (name LIKE 'sqlite_sequence' OR
name LIKE 'android_metadata')
Alternatively, simply use IN with a list:
WHERE type = 'table'
AND name NOT IN ('sqlite_sequence', 'android_metadata')
来源:https://stackoverflow.com/questions/47004250/getting-list-of-table-names-from-database-in-android-application