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 ...
随机推荐
- Nessus简单使用
1.更新插件 上次搭建完后总觉得不踏实,因为老是提示插件多久没更新了,然后果断花了1.25美刀买了台vps,终于把最新的插件下载下来了,总共190M,需要的QQ私信我.
- flask + Python3 实现的的API自动化测试平台---- IAPTest接口测试平台,更名:FXTest 接受定制开发(java版开发完毕)
**背景: 1.平时测试接口,总是现写代码,对测试用例的管理,以及测试报告的管理持久化做的不够, 2.工作中移动端开发和后端开发总是不能并行进行,需要一个mock的依赖来让他 ...
- layui如何自定义弹出层关闭事件
在某些业务场景下,我们需要自定义弹出层关闭事件,代码示例如下: layui.use('layer', function () { var layer = layui.layer; layer.open ...
- vs2015 编译obs studio 遇到的几个错误
1. >D:\project\vs\obs\ObsProject\obs-studio\plugins\win-wasapi\win-wasapi.cpp(245): error C2065: ...
- 运行OpenGL红宝书第9版源码时Visual Studio提示“无法启动程序...ALL_BUILD。拒绝访问“的问题的解决办法
问题描述: OpenGL红宝书第9版源码采用CMake编译后,用相应的Visual Studio(如VS2012)打开“vermilion9.sln”解决方案,并运行时Visual Studio提示“ ...
- IDEA启动Springboot时,解决报错java.lang.NoClassDefFoundError: javax/servlet/Filter
如下所示,将spring-boot-starter-tomcat依赖中的<scope>provided</scope>注释掉 <dependency> <gr ...
- python的subprocess模块介绍
一.subprocess以及常用的封装函数运行python的时候,我们都是在创建并运行一个进程.像Linux进程那样,一个进程可以fork一个子进程,并让这个子进程exec另外一个程序.在Python ...
- word/wps 制作下拉列表
准备: 1.数据页 2.项目名称sheet 3.问题类型sheet 开始制作: 数据 --- 有效性 --- 允许“序列” --- 来源 -- 其他sheet页“单元格”选择范围 回车.确定 即可
- Linux云主机(离线)搭建本地yum源
下载CentOS镜像 http://mirror.nsc.liu.se/centos-store/7.3.1611/isos/x86_64/ 离线yum源的配置 1.上传下载的镜像源iso 2.挂载i ...
- Aspose.Words操作Word.PDF,让图片和文本垂直居中,水平居中解决方案
x 环境 { "Aspose.Words": {"Version":"18.x"} } 需求与难题 生成试卷的时候,如果数学题目中有特殊符号 ...