Dart API
DuckDB.Dart is the native Dart API for DuckDB.
Installation
DuckDB.Dart can be installed from pub.dev. Please see the API Reference for details.
Use This Package as a Library
Depend on It
Run this command:
With Flutter:
flutter pub add dart_duckdb
This will add a line like this to your package's pubspec.yaml (and run an implicit flutter pub get):
dependencies: dart_duckdb: ^1.1.3
Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.
Import It
Now in your Dart code, you can import it:
import 'package:dart_duckdb/dart_duckdb.dart';
Usage Examples
See the example projects in the duckdb-dart repository:
-
cli: command-line application -
duckdbexplorer: GUI application which builds for desktop operating systems as well as Android and iOS.
Here are some common code snippets for DuckDB.Dart:
Querying an In-Memory Database
import 'package:dart_duckdb/dart_duckdb.dart';
void main() {
final db = duckdb.open(":memory:");
final connection = duckdb.connect(db);
connection.execute('''
CREATE TABLE users (id INTEGER, name VARCHAR, age INTEGER);
INSERT INTO users VALUES (1, 'Alice', 30), (2, 'Bob', 25);
''');
final result = connection.query("SELECT * FROM users WHERE age > 28").fetchAll();
for (final row in result) {
print(row);
}
connection.dispose();
db.dispose();
} Queries on Background Isolates
import 'package:dart_duckdb/dart_duckdb.dart';
void main() {
final db = duckdb.open(":memory:");
final connection = duckdb.connect(db);
await Isolate.spawn(backgroundTask, db.transferrable);
connection.dispose();
db.dispose();
}
void backgroundTask(TransferableDatabase transferableDb) {
final connection = duckdb.connectWithTransferred(transferableDb);
// Access database ...
// fetch is needed to send the data back to the main isolate
}
© Copyright 2018–2024 Stichting DuckDB Foundation
Licensed under the MIT License.
https://duckdb.org/docs/api/dart.html