Getting list of table names from database in Android application

血红的双手。 提交于 2019-12-12 04:19:26

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!