Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: Add clearAllTables Function #834

Open
nawinkhatiwada opened this issue Jul 4, 2024 · 0 comments
Open

Feature Request: Add clearAllTables Function #834

nawinkhatiwada opened this issue Jul 4, 2024 · 0 comments

Comments

@nawinkhatiwada
Copy link

In some scenarios, it is necessary to clear all data from all tables in the database, including tables that store auto-increment values (sqlite_sequence). Currently, achieving this requires custom SQL queries and manual management.

I would like to request the addition of a clearAllTables function to the Floor library. This function would clear all data from all tables in the database, including the sqlite_sequence table. The function should be transaction-safe to ensure data integrity.

The alternative is to manually write SQL queries to delete data from all tables, but this approach can be error-prone and less convenient for developers.

Here is a sample implementation of how this might be achieved using the sqflite package in Flutter:

Future<void> clearAllTables(Database db) async {
  final tables = await db.rawQuery("SELECT name FROM sqlite_master WHERE type='table'");

  await db.transaction((txn) async {
    for (var table in tables) {
      var tableName = table['name'];
      await txn.execute('DELETE FROM $tableName');
    }
  });
}

This function retrieves all table names and executes a DELETE FROM statement for each table inside a transaction.

Having a built-in function in the Floor library would make it easier and more reliable for developers to clear their database.

Thank you for considering this feature request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant