卸载flutter windows应用程序时如何删除本地数据库?



我正在使用DRIFT包(以前称为MOOR)作为本地数据库,我想在卸载windows应用程序时删除数据库文件,该应用程序保存在本地机器(windows)中为db.sqlite。我怎样才能做到这一点呢?

数据库漂移包。漂移文档。漂移代码:

// These imports are only needed to open the database
import 'dart:io';
import 'package:drift/drift.dart';
import 'package:drift/native.dart';
import 'package:path_provider/path_provider.dart';
import 'package:path/path.dart' as path;
import 'package:summa_app/database/program_location/program_location_dao.dart';
import 'package:summa_app/database/program_location/program_location_table.dart';
import 'package:summa_app/database/programs/program_dao.dart';
import 'package:summa_app/database/programs/program_table.dart';
part 'summa_database.g.dart';
@DriftDatabase(tables: [ProgramLocation, DbPrograms], daos: [ProgramLocationDao, ProgramsDao])
class SummaDatabase extends _$SummaDatabase {
// we tell the database where to store the data with this constructor
SummaDatabase() : super(_openConnection());
// you should bump this number whenever you change or add a table definition.
// Migrations are covered later in the documentation.
@override
int get schemaVersion => 1;
@override
Future<void> close() async{
await  _openConnection().close();
return super.close();
}
}
LazyDatabase _openConnection() {
// the LazyDatabase util lets us find the right location for the file async.
return LazyDatabase(() async {
// put the database file, called db.sqlite here, into the documents folder
// for your app.
final dbFolder = await getApplicationDocumentsDirectory();
final file = File(path.join(dbFolder.path, 'db.sqlite'));
return NativeDatabase(file);
});
}

这里有两个选项:

  1. 您可以关闭数据库,从设备上删除文件,然后重新打开它。
  2. 可以在不关闭数据库的情况下删除所有表中的所有内容:
Future<void> deleteEverything() {
return transaction(() async {
// you only need this if you've manually enabled foreign keys
// await customStatement('PRAGMA foreign_keys = OFF');
for (final table in allTables) {
await delete(table).go();`enter code here`
}
});
}

最新更新