Home > Software design >  List tables that are made by the user/developer and not by SQLite itself
List tables that are made by the user/developer and not by SQLite itself

Time:10-03

If I run the following command:

select * from sqlite_master where type = \'table\';

I get the tables I created, but also tables like: android_metadata and sqlite_sequence. I would like to have a function in my app what wipes the content of my tables but I rather not touch the tables the database created for itself.

I list all tables, extract their name, filter the list and call a delete from $tablename on the list:

  Future<List<QueryRow>> _getNumberOfExistingTableRows() async {
    List<String> sqliteTableNames = ["android_metadata", "sqlite_sequence"];
    String selectTablesSql = "select * from sqlite_master where type = \'table\';";
    final result = await customWriteReturning(selectTablesSql);
    result.removeWhere((row) => sqliteTableNames.contains(row.data["name"]));
    return result;
  }

   List<String> tableNameList = _queryRowsToTableNames(await _getNumberOfExistingTableRows());
    for (String tableName in tableNameList) {
      await customStatement("delete from $tableName;");
    }

It works, but if SQLite in a future version creates another table for itself, this would wipe that table as well.

CodePudding user response:

Using

SELECT * FROM sqlite_master WHERE type = 'table' AND name NOT LIKE 'sqlite_%' AND name NOT LIKE 'android_%';`

should do the trick.

  • Related