SQLite plugin for Flutter. Supports both iOS and Android.
In your flutter project add the dependency:
dependencies:
...
sqflite: any
For help getting started with Flutter, view the online documentation.
Import sqflite.dart
import 'package:sqflite/sqflite.dart';
Demo code to perform Raw SQL queries
// Get a location using getDatabasesPath
var databasesPath = await getDatabasesPath();
String path = join(databasesPath, 'demo.db');
// Delete the database
await deleteDatabase(path);
// open the database
Database database = await openDatabase(path, version: 1,
onCreate: (Database db, int version) async {
// When creating the db, create the table
await db.execute(
'CREATE TABLE Test (id INTEGER PRIMARY KEY, name TEXT, value INTEGER, num REAL)');
});
// Insert some records in a transaction
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');
});
// Update some record
int count = await database.rawUpdate(
'UPDATE Test SET name = ?, VALUE = ? WHERE name = ?',
['updated name', '9876', 'some name']);
print('updated: $count');
// Get the records
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 the records
count = Sqflite
.firstIntValue(await database.rawQuery('SELECT COUNT(*) FROM Test'));
assert(count == 2);
// Delete a record
count = await database
.rawDelete('DELETE FROM Test WHERE name = ?', ['another name']);
assert(count == 1);
// Close the database
await database.close();
Example using the helpers
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 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();
}
Assuming the following read results:
List<Map<String, dynamic>> records = await db.query('my_table');
Resulting map items are read-only
// get the first record
Map<String, dynamic> mapRead = records.first;
// Update it in memory...this will throw an exception
mapRead['my_column'] = 1;
// Crash... `mapRead` is read-only
You need to create a new map if you want to modify it in memory:
// get the first record
Map<String, dynamic> map = Map<String, dynamic>.from(mapRead);
// Update it in memory now
map['my_column'] = 1;
Don't use the database but only use the Transaction object in a transaction to access the database
await database.transaction((txn) async {
// Ok
await txn.execute('CREATE TABLE Test1 (id INTEGER PRIMARY KEY)');
// DON'T use the database object in a transaction
// this will deadlock!
await database.execute('CREATE TABLE Test2 (id INTEGER PRIMARY KEY)');
});
A transaction is committed if the callback does not throw an error. If an error is thrown, the transaction is cancelled. So to rollback a transaction one way is to throw an exception.
To avoid ping-pong between dart and native code, you can use Batch
:
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();
Getting the result for each operation has a cost (id for insertion and number of changes for update and delete), especially on Android where an extra SQL request is executed. If you don't care about the result and worry about performance in big batches, you can use
await batch.commit(noResult: true);
Warning, during a transaction, the batch won't be committed until the transaction is committed
await database.transaction((txn) async {
var batch = txn.batch();
// ...
// commit but the actual commit will happen when the transaction is committed
// however the data is available in this transaction
await batch.commit();
// ...
});
By default a batch stops as soon as it encounters an error (which typically reverts the uncommitted changes). You can ignore errors so that every successfull operation is ran and committed even if one operation fails:
await batch.commit(continueOnError: true);
In general it is better to avoid using SQLite keywords for entity names. If any of the following name is used:
"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"
the helper will escape the name i.e.
db.query('table')
will be equivalent to manually adding double-quote around the table name (confusingly here named table
)
db.rawQuery('SELECT * FROM "table"');
However in any other raw statement (including orderBy
, where
, groupBy
), make sure to escape the name
properly using double quote. For example see below where the column name group
is not escaped in the columns
argument, but is escaped in the where
argument.
db.query('table', columns: ['group'], where: '"group" = ?', whereArgs: ['my_group']);
No validity check is done on values yet so please avoid non supported types https://www.sqlite.org/datatype3.html
DateTime
is not a supported SQLite type. Personally I store them as
int (millisSinceEpoch) or string (iso8601)
bool
is not a supported SQLite type. Use INTEGER
and 0 and 1 values.
int
num
String
Uint8List
List<int>
is supported but not recommended (slow conversion)continueOrError
for batchesDatabase.isOpen
which becomes false once the database is closedSqlflite.hex
to allow querying on blob fieldsgetDatabasesPath
to use as the base location to create a databasepath
), to use the
old behavior use singleInstance = false
when opening a databaseTransaction.batch
:memory:
path)openReadOnlyDatabase
Transaction.applyBatch
Batch.commit
to use outside a transactionTransaction
mechanism not using Zone (old one still supported for now)Batch.apply
instead of Batch.commit
Database.inTransaction
and Database.synchronized
so that Zones are not used anymoreBatch.query
, Batch.rawQuery
and Batch.execute
--preview-dart-2
onConfigure
to allow for database configuration2018/01/04
example/README.md
Demonstrates how to use the sqflite plugin.
flutter run
Specific app entry point
flutter run -t lib/main.dart
For help getting started with Flutter, view the online documentation.
Add this to your package's pubspec.yaml file:
dependencies:
sqflite: ^1.0.0
You can install packages from the command line:
with Flutter:
$ flutter packages get
Alternatively, your editor might support flutter packages get
.
Check the docs for your editor to learn more.
Now in your Dart code, you can use:
import 'package:sqflite/sqflite.dart';
Version | Uploaded | Documentation | Archive |
---|---|---|---|
1.1.0 | Feb 3, 2019 |
|
|
1.0.0 | Jan 8, 2019 |
|
|
0.13.0+1 | Dec 21, 2018 |
|
|
0.13.0 | Dec 5, 2018 |
|
|
0.12.2+1 | Oct 11, 2018 |
|
|
0.12.2 | Oct 11, 2018 |
|
|
0.12.1 | Sep 20, 2018 |
|
|
0.12.0 | Sep 14, 2018 |
|
|
0.11.2+4 | Sep 13, 2018 |
|
|
0.11.2+3 | Sep 13, 2018 |
|
|
Popularity:
Describes how popular the package is relative to other packages.
[more]
|
100
|
Health:
Code health derived from static analysis.
[more]
|
100
|
Maintenance:
Reflects how tidy and up-to-date the package is.
[more]
|
100
|
Overall:
Weighted score of the above.
[more]
|
100
|
We analyzed this package on Feb 14, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:
Detected platforms: Flutter
References Flutter, and has no conflicting libraries.
Package | Constraint | Resolved | Available |
---|---|---|---|
Direct dependencies | |||
Dart SDK | >=2.0.0 <3.0.0 | ||
flutter | 0.0.0 | ||
path | >=1.5.1 <3.0.0 | 1.6.2 | |
synchronized | >=1.5.1 <3.0.0 | 2.0.2+1 | |
Transitive dependencies | |||
collection | 1.14.11 | ||
meta | 1.1.6 | 1.1.7 | |
sky_engine | 0.0.99 | ||
typed_data | 1.1.6 | ||
vector_math | 2.0.8 | ||
Dev dependencies | |||
flutter_test |