db_exporter 0.2.0
db_exporter: ^0.2.0 copied to clipboard
WAL-safe export of SQLite-backed Flutter databases (Drift, sqflite) to raw .db, CSV, JSON or Excel, delivered to a device folder, the share sheet or a save dialog.
db_exporter #
Let your users export their data. Turn your app's SQLite database into a backup file, a spreadsheet, or a share-sheet attachment in one line.
Features #
What you can offer your users:
- πΎ Back up their data β a real
.dbfile they can keep, and you can restore from later. - π Send their data to a spreadsheet β Excel or CSV, ready to open.
- π€ Share it anywhere β WhatsApp, email, Google Drive, Files.
- π Save it where they choose β system file picker, no permissions.
- π± Move to a new phone β export, transfer, reopen.
Works with sqflite, sqlite3 and drift. Android and iOS.
Getting started #
flutter pub add db_exporter
Connect your database once:
final exporter = DbExporter(
DbSource(
databasePath: db.path,
query: db.rawQuery,
execute: db.execute,
),
);
Using drift or sqlite3 instead?
// drift β also drift_sqflite
DbSource(
databasePath: dbFile.path,
query: (sql) async =>
(await db.customSelect(sql).get()).map((row) => row.data).toList(),
execute: db.customStatement,
);
// sqlite3 β also sqlite_async and powersync, with getAll() for select()
DbSource(
databasePath: path,
query: (sql) async =>
db.select(sql).map<Map<String, Object?>>((r) => {...r}).toList(),
execute: (sql) async => db.execute(sql),
);
Usage #
Export a file #
await exporter.exportExcel(); // .xlsx a sheet per table
await exporter.exportCsv(); // .csv a file per table
await exporter.exportJson(); // .json
await exporter.exportDatabaseFile(); // .db reopens in your app
Put it on a button:
ElevatedButton(
onPressed: () => exporter.exportExcel(),
child: const Text('Export my data'),
)
Choose where it goes #
DbExporter(source, destination: const ExportDestination.share());
| Destination | What the user sees |
|---|---|
share() |
The share sheet β pick WhatsApp, email, Drive |
saveAs() |
A file picker β they choose the folder |
deviceFolder() |
Saved to dbexports-<yourapp> on the device (default) |
appDirectory() |
Nothing β stays inside the app |
Export only what you want #
await exporter.exportExcel(
tables: ['orders'], // just these
excludeTables: ['cache'], // everything but these
maxRowsPerTable: 50000,
fileName: 'my_report',
);
Show the outcome #
final result = await exporter.exportCsv();
if (result.userCancelled) return;
showSnackBar('Exported ${result.totalRows} rows');
Failures throw DbExportException, with a message you can show as-is:
try {
await exporter.exportExcel();
} on DbExportException catch (e) {
showSnackBar(e.message);
}
Additional information #
Backups are safe. .db exports use SQLite's VACUUM INTO, so the copy is
consistent even while your app is writing β a plain File.copy loses whatever
is still in the WAL.
CSV is injection-safe. Cells starting with =, +, - or @ are
neutralised so spreadsheets don't execute them.
Android 11+: the default deviceFolder() destination needs All files
access, which Google Play only grants file managers and backup apps. Use
saveAs() instead β it reaches the same places with no permission.
Large tables: Excel, CSV and JSON build rows in memory. Use
maxRowsPerTable, or export the .db.
Not included: encryption, redaction, and support for Hive, Isar or ObjectBox β those have no tables to export.
See example/ for a runnable app exporting three databases, and
./tool/verify.sh to run every export path against a connected device.
License #
MIT