同时支持android和ios

支持事务和批量操作
支持插入/查询/更新/删除操作
在iOS和Android上的后台线程中执行数据库操作

1.添加依赖

dependencies:
...
sqflite: any
Dart

2.导入依赖

import 'package:sqflite/sqflite.dart';
Dart

3.支持SQL查询

// 获取本地SQLite数据库
var databasesPath = await getDatabasesPath();
String path = join(databasesPath, "demo.db"); // 删除数据库
await deleteDatabase(path); // 打开数据库
Database database = await openDatabase(path, version: 1,
onCreate: (Database db, int version) async {
// 当打开数据库的时候创建一张表
await db.execute(
"CREATE TABLE Test (id INTEGER PRIMARY KEY, name TEXT, value INTEGER, num REAL)");
}); // 开启事务,增加两条记录
await database.transaction((txn) async {
int id1 = await txn.rawInsert(
'INSERT INTO Test(name, value, num) VALUES("some name", 1234, 456.789)');
print("inserted1: $id1");
int id2 = await txn.rawInsert(
'INSERT INTO Test(name, value, num) VALUES(?, ?, ?)',
["another name", 12345678, 3.1416]);
print("inserted2: $id2");
}); // 更新一条记录
int count = await database.rawUpdate(
'UPDATE Test SET name = ?, VALUE = ? WHERE name = ?',
["updated name", "9876", "some name"]);
print("updated: $count"); // 获取Test表的数据
List<Map> list = await database.rawQuery('SELECT * FROM Test');
List<Map> expectedList = [
{"name": "updated name", "id": 1, "value": 9876, "num": 456.789},
{"name": "another name", "id": 2, "value": 12345678, "num": 3.1416}
];
print(list);
print(expectedList);
assert(const DeepCollectionEquality().equals(list, expectedList)); // 获取记录的数量
count = Sqflite
.firstIntValue(await database.rawQuery("SELECT COUNT(*) FROM Test"));
assert(count == 2); // 删除一条记录
count = await database
.rawDelete('DELETE FROM Test WHERE name = ?', ['another name']);
assert(count == 1); // 关闭数据库
await database.close();
Dart

4.用法示例

final String tableTodo = "todo";
final String columnId = "_id";
final String columnTitle = "title";
final String columnDone = "done"; class Todo {
int id;
String title;
bool done; Map<String, dynamic> toMap() {
var map = <String, dynamic>{
columnTitle: title,
columnDone: done == true ? 1 : 0
};
if (id != null) {
map[columnId] = id;
}
return map;
} Todo(); Todo.fromMap(Map<String, dynamic> map) {
id = map[columnId];
title = map[columnTitle];
done = map[columnDone] == 1;
}
} class TodoProvider {
Database db; Future open(String path) async {
db = await openDatabase(path, version: 1,
onCreate: (Database db, int version) async {
await db.execute('''
create table $tableTodo (
$columnId integer primary key autoincrement,
$columnTitle text not null,
$columnDone integer not null)
''');
});
} Future<Todo> insert(Todo todo) async {
todo.id = await db.insert(tableTodo, todo.toMap());
return todo;
} Future<Todo> getTodo(int id) async {
List<Map> maps = await db.query(tableTodo,
columns: [columnId, columnDone, columnTitle],
where: "$columnId = ?",
whereArgs: [id]);
if (maps.length > 0) {
return new Todo.fromMap(maps.first);
}
return null;
} Future<int> delete(int id) async {
return await db.delete(tableTodo, where: "$columnId = ?", whereArgs: [id]);
} Future<int> update(Todo todo) async {
return await db.update(tableTodo, todo.toMap(),
where: "$columnId = ?", whereArgs: [todo.id]);
} Future close() async => db.close();
}
Dart

注意事项

1.Transaction(事务)

当使用transaction对象访问数据时,不能再使用database对象访问数据库
await database.transaction((txn) async {
// 完全Ok
await txn.execute("CREATE TABLE Test1 (id INTEGER PRIMARY KEY)"); // 不能在transaction对象里面使用database对象,这会发生死锁
// await database.execute("CREATE TABLE Test2 (id INTEGER PRIMARY KEY)");
});
Dart

2.支持批量操作

batch = db.batch();
batch.insert("Test", {"name": "item"});
batch.update("Test", {"name": "new_item"}, where: "name = ?", whereArgs: ["item"]);
batch.delete("Test", where: "name = ?", whereArgs: ["item"]);
results = await batch.commit();
Dart
这些操作返回结果,都会有一些开销;如果你不考虑结果,可以使用:
await batch.commit(noResult: true);
Dart
事务期间,直到事务被提交,批量操作才能提交
await database.transaction((txn) async {
var batch = txn.batch();
//...
// 实际提交将在事务启动时发生
await batch.commit();
});
Dart

3.表和列的名字

通常避免使用SQLite的关键字作为表名或者列名,例如以下其中之一:
"add","all","alter","and","as","autoincrement","between","case","check","collate","commit","constraint","create","default","deferrable","delete","distinct","drop","else","escape","except","exists","foreign","from","group","having","if","in","index","insert","intersect","into","is","isnull","join","limit","not","notnull","null","on","or","order","primary","references","select","set","table","then","to","transaction","union","unique","update","using","values","when","where"
Dart
否则要使用双引号转义,例如:
db.rawQuery('SELECT * FROM "table"');
db.query("table", columns: ["group"], where: '"group" = ?', whereArgs: ["my_group"]);
Dart

4.支持的列类型

INTEGER   相当于dart类型中int
REAL 相当于dart类型中num
TEXT 相当于dart类型中String
BLOB 相当于dart类型中Uint8List

Flutter中SQLite数据库的使用的更多相关文章

  1. Android中SQLite数据库操作(1)——使用SQL语句操作SQLite数据库

    下面是最原始的方法,用SQL语句操作数据库.后面的"Android中SQLite数据库操作(2)--SQLiteOpenHelper类"将介绍一种常用的android封装操作SQL ...

  2. Android中SQLite数据库小计

    2016-03-16 Android数据库支持 本文节选并翻译<Enterprise Android - Programing Android Database Applications for ...

  3. 我的Android六章:Android中SQLite数据库操作

    今天学习的内容是Android中的SQLite数据库操作,在讲解这个内容之前小编在前面有一篇博客也是讲解了SQLite数据库的操作,而那篇博客的讲解是讲述了 如何在Window中通过DOM来操作数据库 ...

  4. Android 中 SQLite 数据库的查看

    当 SQLite 数据库创建完成后,如何查看数据库的内容呢?如果直接使用 File Explorer 查看,最多只能看到 database 目录下出现了一个 BookStore.db 文件,Book ...

  5. iOS 中SQLite数据库操作

    在iOS中实现SQLite数据库的操作:1.导入框架(libsqlite3.0.tbd) 2.导入头文件<sqlite3.h> 3.实现数据的增删改查 实现简单 SQLite数据库操作 的 ...

  6. Android中Sqlite数据库进行增删改查

    今天这篇文章写Sqlite数据库,通过一个小案例来完整讲一下数据库常见的CRUD操作. 先对知识点总结: SQLite数据库 轻量级关系型数据库 创建数据库需要使用的api:SQLiteOpenHel ...

  7. Android 开发中 SQLite 数据库的使用

    SQLite 介绍 SQLite 一个非常流行的嵌入式数据库,它支持 SQL 语言,并且只利用很少的内存就有很好的性能.此外它还是开源的,任何人都可以使用它.许多开源项目((Mozilla, PHP, ...

  8. Android中Sqlite数据库多线程并发问题

    最近在做一个Android项目, 为了改善用户体验,把原先必须让用户“等待”的过程改成在新线程中异步执行.但是这样做遇到了多个线程同时需要写Sqlite数据库,导致操作数据库失败. 本人对Java并不 ...

  9. android中sqlite数据库的基本使用和添加多张表

    看了很多关于android使用sqlite数据库的文章,很多都是介绍了数据库的建立和表的建立,而表通常都是只建立一张,而实际情况我们用到的表可能不止一张,那这种情况下我们又该怎么办呢,好了,下面我教大 ...

随机推荐

  1. springboot/Mybatis整合

    正题 本项目使用的环境: 开发工具:Intellij IDEA 2017.1.3 springboot: 1.5.6 jdk:1.8.0_161 maven:3.3.9 额外功能 PageHelper ...

  2. java常用工具包

    1.国外 org.apache.commons.lang3 http://commons.apache.org/proper/commons-lang/javadocs/api-3.8.1/index ...

  3. 14 - How to check replication status

    The people using PostgreSQL and the Streaming Replication feature seem to ask many of the same quest ...

  4. VS2015调试ArcMap Add-in插件提示尝试运行项目时出错,无法启动程序“路径\arcmap.exe”

    如图所示的错误: 打开VS2015→工具→选项→常规→如图标记的两项打钩: 参考: https://gis.stackovernet.com/cn/q/40845 http://www.voidcn. ...

  5. VUE错误码Attribute ':sizeOpts' must be hyphenated

    Attribute ':sizeOpts' must be hyphenated 因为属性有大写,需要添加 - 来取代 例如 tampData  换成 tamp-data 就可以了

  6. Servlet(自己实现的Servlet)细节

    Java中无状态的对象就是指某种没用任何属性的仅仅由方法组成的对象. *无状态:无状态方法的好处之一,就是在各种环境下,都可以安全调用.衡量一个方法 是否有状态的,就看它是否改动了其他东西. *有状态 ...

  7. git rebase和git merge的用法

    http://softlab.sdut.edu.cn/blog/subaochen/2016/01/git-rebase%E5%92%8Cgit-merge%E7%9A%84%E7%94%A8%E6% ...

  8. poj2362

    #include<iostream> using namespace std; ]; int total; int rec; int n; ]; int flag; int flag1; ...

  9. LeetCode 217 Contains Duplicate 解题报告

    题目要求 Given an array of integers, find if the array contains any duplicates. Your function should ret ...

  10. org.hibernate.HibernateException: Duplicate identifier in table for: Waa

    提示表的标识符重复,发现是数据库中的主键id重复了.因为是序列自动生成的. 我原本以为是因为我的序列的问题,序列.nextval()有问题,但是当我在数据库测试时,发现当前序列没有问题.但是当数据插入 ...