如何在flutter中打开SQLite数据库?

9rnv2umw  于 2023-04-21  发布在  SQLite
关注(0)|答案(1)|浏览(181)

我需要一种方法来打开一个已经构建好的数据库。我已经使用一些python脚本创建了数据库,我需要在我的Flutter应用程序上使用这个数据库,但是当我试图打开存储在我的android手机中的数据库时,它告诉我它不可读。
下面是我的代码:

Future<Database> _initDatabase() async {
  print('1');
  Database database = await openDatabase(
    '/storage/emulated/0/Etudes/PFE/Implementation/database/example.db',
    version: 1,
  );
  print('2');
  return database;
}

@override
void initState() {
  super.initState();
  _initDatabase();
}

当我运行它的时候,它给我这个错误:
E/SQLiteLog(19872):(14)无法打开[d2 e6722037]第38590行的文件
E/SQLiteLog(19872):(14)os_unix.c:38590:(13)open(/storage/emulated/0/Etudes/PFE/Implementation/database/example.db)-
E/SQLiteDatabase(19872):android.database.sqlite.SQLiteCantOpenDatabaseException:无法打开数据库“/storage/emulated/0/Etudes/PFE/Implementation/database/example.db”:文件/storage/emulated/0/Etudes/PFE/Implementation/database/example. db不可读

dphi5xsq

dphi5xsq1#

你能确认该文件存在于给定的链接下吗?
无论如何,我建议你不要像这样硬编码你的路径。最好使用给定的Android-call来获取外部存储的路径。我建议检查path_provider包以获得flutter。(参见:https://pub.dev/documentation/path_provider/latest/path_provider/getExternalStorageDirectories.html

相关问题