Sqlite in flutter, how database assets work
First off, you will need to construct a sqlite database from your csv. This can be done in the following way:
Create the necessary table (users.sql)
CREATE TABLE users(
firstname TEXT NOT NULL,
lastname TEXT NOT NULL,
dob TEXT NOT NULL
);Create the sqlite database
sqlite3 database.db < users.sqlInsert the csv data
sqlite3 database.db
.mode csv
.import data.csv usersPut database.db into your assets and add that in pubspec.yaml.
flutter:
# ...
assets:
- assets/database.dbIn your app, you'll have to copy the asset file into "documents". This is slightly complicated.
// Construct a file path to copy database to
Directory documentsDirectory = await getApplicationDocumentsDirectory();
String path = join(documentsDirectory.path, "asset_database.db"); // Only copy if the database doesn't exist
if (FileSystemEntity.typeSync(path) == FileSystemEntityType.notFound){
// Load database from asset and copy
ByteData data = await rootBundle.load(join('assets', 'database.db'));
List<int> bytes = data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes); // Save copied asset to documents
await new File(path).writeAsBytes(bytes);
}Lastly, you can access the database like so.
Directory appDocDir = await getApplicationDocumentsDirectory();
String databasePath = join(appDocDir.path, 'asset_database.db');
this.db = await openDatabase(databasePath);
initialized = true;Example query (this._initialize() is step 6)
Future<List<Page>> search(String word, int parentId) async {
if (!initialized) await this._initialize();
String query = '''
SELECT * FROM users
LIMIT 25''';
return await this.db.rawQuery(query);
}
Sqlite in flutter, how database assets work的更多相关文章
- pycharm .sqlite文件拖动到Database里面为空
pycharm .sqlite文件拖动到Database里面为空 查资料得到解决方法:
- sqlite 报错:database is locked
在sqlite批量添加数据时,报错:database is locked. 解决办法:将db路径由相对路径设置为绝对路径.
- Sqlite出现SQL error: database disk image is malformed的处理
SQLite有一个很严重的缺点就是不提供Repair命令.导致死亡提示database disk image is malformed它的产生有很多种可能,比如,磁盘空间不足,还有就是写入数据过程中突 ...
- PHP PDO sqlite ,Unable to Open database file的解决方法
t.php在网站的根目录. fdy.db在inc文件夹下; t.php中sqlite路径写成相对路径 $db = new PDO('sqlite:inc/fdy.db'); 开始提示 Fatal er ...
- SQLITE 多进程查询出错database is locked
程序比较简单: 父进程查询数据库A表,没有更新操作 子进程同时查询数据库A表,查询出来的内容更新B表. 两个进程都放到while(1)循环中,速度慢的话就是2S执行一次就没有错,执行的速度快的话就会报 ...
- sqlite 数据库错误 The database disk image is malformed database disk image
收银机上的sqlite数据库经常出现这种错误,错误的原因有可能是突然断电或是一些不规范操作导致的. 网上一般的做法有两种: 方法一: 1.在https://www.sqlite.org/downloa ...
- Android SQLite Database Tutorial
表名: 列(字段): 联系人实体类:构造方法,setters .getters方法 File: Contact.java package com.example.sqlitetest; publi ...
- Using SQLite database in your Windows 10 apps
MVP可以在channel 9上传视频了,所以准备做个英文视频传上去分享给大家,本文做稿子. Hello everyone, As we all know, SQLite is a great and ...
- android 同时打开两个sqlite database db
1,数据库类 package com.example.testdb; import android.content.Context; import android.database.SQLExcept ...
随机推荐
- Redis 下载 安装
Redis 官网 https://redis.io/ github 主页 https://github.com/antirez/redis 下载页面 https://redis.io/download ...
- 2019SDN第7次上机作业
2019SDN第7次上机作业 1.作业要求: 作业博客链接:https://edu.cnblogs.com/campus/fzu/fzusdn2019/homework/10165 2.具体操作步骤与 ...
- 导入eclipse有Unbound classpath variable: 'M2_REPO报错的解决方法
Eclipse maven of the project reported in Unbound classpath variable: 'M2_REPO /**/***/***. jar' But ...
- idea在使用git clone 时出现Filename too long
idea在使用git clone 时出现Filename too long的报错信息,使用如下命令就可以解决该问题:在 git bash命令模式下,运行命令 git config --global c ...
- Windows安装pip、wxpy
版权归作者所有,任何形式转载请联系作者.作者:为什么不是学霸(来自豆瓣)来源:https://www.douban.com/note/696046743/ # 适用于0基础 1.安装好python. ...
- Learning Context Graph for Person Search
Learning Context Graph for Person Search 2019-06-24 09:14:03 Paper:http://openaccess.thecvf.com/cont ...
- [LINUX] 快速回收连接
i /etc/sysctl.conf 编辑文件,加入以下内容:net.ipv4.tcp_syncookies = 1net.ipv4.tcp_tw_reuse = 1net.ipv4.tcp_tw_r ...
- 微信小程序开发——使用第三方插件生成二维码
需求场景: 小程序中指定页面需要根据列表数据生成多张二维码. 实现方案: 鉴于需要生成多张二维码,可以将生成二维码的功能封装到组件中,直接在页面列表循环中调用就好了.也可以给组件添加slot,在页面调 ...
- docker安装并运行redis
拉取镜像: [mall@VM_0_7_centos ~]$ sudo docker pull redis:3.2 [sudo] password for mall: 3.2: Pulling from ...
- [LeetCode] 88. Merge Sorted Array 合并有序数组
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: T ...