forked from simolus3/sqlite3.dart
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.dart
52 lines (42 loc) · 1.5 KB
/
main.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import 'dart:io';
import 'package:sqlite3/sqlite3.dart';
void main() {
print('Using sqlite3 ${sqlite3.version}');
// Create a new in-memory database. To use a database backed by a file, you
// can replace this with sqlite3.open(yourFilePath).
final db = sqlite3.openInMemory();
// Create a table and insert some data
db.execute('''
CREATE TABLE artists (
id INTEGER NOT NULL PRIMARY KEY,
name TEXT NOT NULL
);
''');
// Prepare a statement to run it multiple times:
final stmt = db.prepare('INSERT INTO artists (name) VALUES (?)');
stmt
..execute(['The Beatles'])
..execute(['Led Zeppelin'])
..execute(['The Who'])
..execute(['Nirvana']);
// Dispose a statement when you don't need it anymore to clean up resources.
stmt.dispose();
// You can run select statements with PreparedStatement.select, or directly
// on the database:
final ResultSet resultSet =
db.select('SELECT * FROM artists WHERE name LIKE ?', ['The %']);
// You can iterate on the result set in multiple ways to retrieve Row objects
// one by one.
for (final Row row in resultSet) {
print('Artist[id: ${row['id']}, name: ${row['name']}]');
}
// Register a custom function we can invoke from sql:
db.createFunction(
functionName: 'dart_version',
argumentCount: const AllowedArgumentCount(0),
function: (args) => Platform.version,
);
print(db.select('SELECT dart_version()'));
// Don't forget to dispose the database to avoid memory leaks
db.dispose();
}