[英]Use existing sqlite database in flutter app
我是 dart 和 flutter 的新手,我正在尝试创建一个使用现有 sqlite 数据库的应用程序。 我已将资产包含在我的 pubspec.yaml 文件中。 当我运行该应用程序时,我现有数据库中的任何数据都不会显示。 看起来它每次都在创建一个新表。 这是我复制表格的代码。
Future<Database> _initDB(String filePath) async {
final dbPath = await getDatabasesPath();
final path = join(dbPath, filePath);
var exists = await databaseExists(path);
if (!exists) {
print("Creating a new copy from assets");
// Make sure the parent directory exists
try {
await Directory(dirname(path)).create(recursive: true);
print("Creating directory");
} catch (_) {
print('Error creating Directory');
}
// Copy from asset
print("Copying DB");
ByteData data = await rootBundle.load(join("assets", "notes.db"));
List<int> bytes =
data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
// Write and flush the bytes written
await File(path).writeAsBytes(bytes, flush: true);
print('DB has been copied');
} else {
print("Opening existing database");
}
return await openDatabase(path, version: 1, onCreate: _createDB);
}
有人有主意吗? 谢谢!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.