栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Flutter sqflite打开现有数据库

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Flutter sqflite打开现有数据库

《打开资产数据库》指南介绍了在Flutter应用程序中捆绑并打开预先存在的SQLite数据库所需执行的步骤:

首先,您必须编辑

pubspec.yaml
配置以引用现有的SQLite数据库文件,以便在构建应用程序时将其捆绑到您的应用程序中。在以下示例中,我们假定文件存在于
assets/demo.db
Flutter应用程序目录下:

# The following section is specific to Flutter.flutter:  # The following line ensures that the Material Icons font is  # included with your application, so that you can use the icons in  # the material Icons class.  uses-material-design: true  # To add assets to your application, add an assets section, like this:  assets:    - assets/demo.db

接下来,在应用程序初始化中,您将需要将捆绑的文件数据复制到可用位置,因为捆绑的资源本身无法在Android上直接作为文件打开。

sqflite.getDatabasesPath()
函数将返回用于此目的的目录。这通常类似于
/data/data/org.example.myapp/databases/
Android。有了这个,您就可以从捆绑资产中加载字节数据,并创建应用程序的可写数据库文件,此处名为
app.db

// Construct the path to the app's writable database file:var dbDir = await getDatabasesPath();var dbPath = join(dbDir, "app.db");// Delete any existing database:await deleteDatabase(dbPath);// Create the writable database file from the bundled demo database file:ByteData data = await rootBundle.load("assets/demo.db");List<int> bytes = data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);await File(dbPath).writeAsBytes(bytes);

最后,您可以在应用启动时打开创建的数据库文件:

var db = await openDatabase(dbPath);


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/437284.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号