sqflite是github上tekartik提供的可供Android和iOS使用的SQLite插件,操做也是很是的簡單.git
下載
dependencies: ... sqflite: ^1.1.0
導入
import 'package:sqflite/sqflite.dart';
用SQL進行操做
獲取資源存放位置
import 'package:path/path.dart'; // join 使用這個包 String _path = ''; Future getPath() async { return await getDatabasesPath(); } // 使用 getPath().then((path) { setState(() { _path = join(path.toString(), 'demo.db'); }); });
建表
Database _database; Future createDB() async { _database = await openDatabase( _path, version: 1, onCreate: (db, version) async { await db.execute( 'CREATE TABLE demo(id INTEGER PRIMARY KEY, name TEXT, value INTEGER, num REAL)'); }, ); }
插入一條數據
_database.transaction((txn) async { await txn.rawInsert( 'INSERT INTO demo(name, value, num) VALUES("some name", 1234, 456.789)'); })
查詢數據
Future<List> query() async { return await _database.rawQuery('SELECT * FROM demo'); }
修改數據
Future update() async { return await _database.rawUpdate( 'UPDATE demo SET name = ?, VALUE = ? WHERE name = ?', ['updated name', '9876', 'some name']); }
刪除一條數據
Future delete() async { return await _database .rawDelete('DELETE FROM demo WHERE 1 ORDER BY id LIMIT 1'); }