如何在颤抖中阅读“老”的sqlite?

时间:2019-05-21 09:26:03

标签: android sqlite flutter

我使用flutter重写了现有的android应用。

是否可以访问旧应用程序创建的旧数据库?如果我在旧表上进行SELECT,则不会得到任何数据。

旧应用:

private final static String DB_NAME = "oweapp";

新应用:

String path = join(documentsDirectory.path, "oweapp.db");

1 个答案:

答案 0 :(得分:0)

如果在资产文件夹中提供数据库,则应首先将数据库复制到设备。如果没有,则应该在设备的外部存储器中打开数据库(如您所进行的那样)。

ByteData data = await rootBundle.load("assets/oweapp.db");
List<int> bytes = data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);

final directory = await getDatabasesPath();
String dbPath = join(directory, "oweapp.db");
await File(dbPath).writeAsBytes(bytes);

// After that you can do:
var db = await openDatabase(dbPath);
相关问题