This is a Flutter plugin that bundles and wraps SQLCipher for Android, an open-source extension to SQLite that provides transparent 256-bit AES encryption of database files.
-
Implements fully-encrypted SQLite databases stored on disk or in memory.
-
Supports booleans, doubles, integers, strings, blobs, and timestamps.
-
Provides a high-fidelity subset of the
android.database.sqlite
API to aid Android developers migrating to Flutter. -
Facilitates porting existing Android database code to Flutter.
Android only, at present. (iOS support is planned.)
await db.execSQL("DROP TABLE IF EXISTS links");
// Create a bookmark links table:
await db.execSQL("""
CREATE TABLE links (
id INTEGER PRIMARY KEY NOT NULL,
url TEXT NOT NULL,
created_at DATETIME NOT NULL,
updated_at DATETIME NULL
)
""");
// Insert a new link into the table:
var linkID = db.insert(
table: "links",
values: <String, dynamic>{
"id": null, // auto-incremented ID assigned automatically
"url": "http://example.org/",
"created_at": DateTime.now().toUtc().millisecondsSinceEpoch ~/ 1000,
"updated_at": null,
},
);
// Change the previously-inserted link from HTTP to HTTPS:
db.update(
table: "links",
values: <String, dynamic>{
"url": "https://example.org/",
"updated_at": DateTime.now().toUtc().millisecondsSinceEpoch ~/ 1000,
},
where: "id = ?",
whereArgs: <String>[linkID.toString()],
);
// Delete the previously-inserted link:
db.delete(
table: "links",
where: "id = ?",
whereArgs: <String>[linkID.toString()],
);
for (var row in await db.rawQuery("SELECT 1 AS a, 2 as b, 3 AS c")) {
print(row); // prints: {a: 1, b: 2, c: 3}
}
import 'package:flutter_sqlcipher/sqlite.dart';
var db = await SQLiteDatabase.createInMemory();
This example also uses
Context
from the flutter_android package
to obtain the app's cache directory path.
import 'package:flutter_sqlcipher/sqlite.dart';
import 'package:flutter_android/android_content.dart' show Context;
var cacheDir = await Context.cacheDir;
await cacheDir.create(recursive: true);
var cacheFile = File("${cacheDir.path}/cache.db");
var db = await SQLiteDatabase.openOrCreateDatabase(cacheFile.path);
(To be added.)
SQLCipher for Android 4.0.0, SQLCipher 4.0.0, and SQLite 3.25.2.
Two good reasons are:
-
Encryption. Android's native SQLite support does not feature database encryption. By using this plugin, you can trivially enable encryption for your app database, something likely appreciated by both you as well as your users.
-
Compatibility. Android's native SQLite version varies greatly depending on the specific Android release, from SQLite 3.4 (released in 2007) to SQLite 3.19 (released in 2017, bundled in Android 8.1). Further, some device manufacturers include different versions of SQLite on their devices. By using this plugin, you gain a consistent, predictable, and up-to-date version of SQLite for your app regardless of the Android release your app runs on.
Due to the bundled SQLCipher native libraries, your final APK size currently
increases by about 6.7 MiB. We are actively
investigating
ways to reduce that footprint. (e.g.,
pruning .so
files
and using ProGuard).
We don't generally implement methods deprecated in the current Android API
level. For example, the
SQLiteDatabase#isDbLockedByOtherThreads()
method was deprecated long ago (in Android 4.1), so we have omitted it from
the Dart interface when implementing this plugin.
-
At present, iOS is not supported. This may eventually be addressed going forward by bundling and wrapping FMDB which includes SQLCipher support.
-
At present, cursors are fully materialized. This means that queries which return very large result sets will incur nontrivial overhead in the IPC transfer of the cursor data from Java to Dart. We are planning on adding windowed cursor and streaming support in a future major release. In the meanwhile,
OFFSET
andLIMIT
are your friends.
import 'package:flutter_sqlcipher/sqlcipher.dart';
import 'package:flutter_sqlcipher/sqlite.dart';
SQLite.version
SQLiteCursor
SQLiteDatabase
SQLiteDatabase.create()
SQLiteDatabase.createInMemory()
SQLiteDatabase.deleteDatabase()
SQLiteDatabase.openDatabase()
SQLiteDatabase.openOrCreateDatabase()
SQLiteDatabase.releaseMemory()
SQLiteDatabase#inTransaction
SQLiteDatabase#maximumSize
SQLiteDatabase#pageSize
SQLiteDatabase#path
SQLiteDatabase#version
SQLiteDatabase#beginTransaction()
SQLiteDatabase#beginTransactionNonExclusive()
SQLiteDatabase#close()
SQLiteDatabase#delete()
SQLiteDatabase#disableWriteAheadLogging()
SQLiteDatabase#enableWriteAheadLogging()
SQLiteDatabase#endTransaction()
SQLiteDatabase#execSQL()
SQLiteDatabase#getAttachedDbs()
SQLiteDatabase#getMaximumSize()
SQLiteDatabase#getPageSize()
SQLiteDatabase#getPath()
SQLiteDatabase#getVersion()
SQLiteDatabase#insert()
SQLiteDatabase#insertOrThrow()
SQLiteDatabase#insertWithOnConflict()
SQLiteDatabase#isDatabaseIntegrityOk
SQLiteDatabase#isDbLockedByCurrentThread
SQLiteDatabase#isOpen
SQLiteDatabase#isReadOnly
SQLiteDatabase#isWriteAheadLoggingEnabled
SQLiteDatabase#needUpgrade()
SQLiteDatabase#query()
SQLiteDatabase#rawQuery()
SQLiteDatabase#replace()
SQLiteDatabase#replaceOrThrow()
SQLiteDatabase#setForeignKeyConstraintsEnabled()
SQLiteDatabase#setLocale()
SQLiteDatabase#setMaxSqlCacheSize()
SQLiteDatabase#setMaximumSize()
SQLiteDatabase#setPageSize()
SQLiteDatabase#setTransactionSuccessful()
SQLiteDatabase#setVersion()
SQLiteDatabase#update()
SQLiteDatabase#updateWithOnConflict()
SQLiteDatabase#validateSql()
SQLiteDatabase#yieldIfContendedSafely()
Dart Class | Dart API | SQLite Storage Class | Notes |
---|---|---|---|
null |
SQLiteCursor#isNull() |
NULL |
- |
bool |
SQLiteCursor#getBool() |
INTEGER |
0 , 1 |
int |
SQLiteCursor#getInt() |
INTEGER |
- |
double |
SQLiteCursor#getDouble() |
REAL |
- |
String |
SQLiteCursor#getString() |
TEXT |
- |
Uint8List |
SQLiteCursor#getBlob() |
BLOB |
- |
DateTime |
SQLiteCursor#getDateTime() |
TEXT |
ISO-8601 "YYYY-MM-DD HH:MM:SS.SSS" |
DateTime |
SQLiteCursor#getDateTime() |
INTEGER |
Seconds since 1970-01-01T00:00:00Z |
- The sql_builder package implements a fluent DSL interface for constructing SQL queries.