同時支持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]);