### Basic SQLite Database Operations with sqflite_common_ffi Source: https://pub.dev/packages/sqflite_common_ffi/example Demonstrates initializing the FFI loader, opening an in-memory database, creating a table, inserting data, querying data, and closing the database. This is a fundamental example for getting started with database operations. ```dart // ignore_for_file: avoid_print import 'package:sqflite_common_ffi/sqflite_ffi.dart'; Future main() async { // Init ffi loader if needed. sqfliteFfiInit(); var databaseFactory = databaseFactoryFfi; var db = await databaseFactory.openDatabase(inMemoryDatabasePath); await db.execute(''' CREATE TABLE Product ( id INTEGER PRIMARY KEY, title TEXT ) '''); await db.insert('Product', {'title': 'Product 1'}); await db.insert('Product', {'title': 'Product 1'}); var result = await db.query('Product'); print(result); // prints [{id: 1, title: Product 1}, {id: 2, title: Product 1}] await db.close(); } ``` -------------------------------- ### Setup sqlite3.wasm and sqflite_sw.js Binaries Source: https://pub.dev/packages/sqflite_common_ffi_web Run the setup command to install necessary binaries into your web folder. Use the --force option to re-download if binaries are updated. ```bash $ dart run sqflite_common_ffi_web:setup ``` ```bash $ dart run sqflite_common_ffi_web:setup --force ``` -------------------------------- ### Linux setup for sqflite_common_ffi Source: https://pub.dev/packages/sqflite_common_ffi Run the provided script or install necessary packages for Linux. ```bash dart tool/linux_setup.dart ``` ```bash sudo apt-get -y install libsqlite3-0 libsqlite3-dev ``` -------------------------------- ### Basic sqflite FFI application example Source: https://pub.dev/packages/sqflite_common_ffi Demonstrates opening an in-memory database, creating a table, inserting data, querying it, and closing the database using sqflite_common_ffi. ```dart import 'package:sqflite_common/sqlite_api.dart'; import 'package:sqflite_common_ffi/sqflite_ffi.dart'; Future main() async { // Init ffi loader if needed. sqfliteFfiInit(); var databaseFactory = databaseFactoryFfi; var db = await databaseFactory.openDatabase(inMemoryDatabasePath); await db.execute(''' CREATE TABLE Product ( id INTEGER PRIMARY KEY, title TEXT ) '''); await db.insert('Product', {'title': 'Product 1'}); await db.insert('Product', {'title': 'Product 1'}); var result = await db.query('Product'); print(result); // prints [{id: 1, title: Product 1}, {id: 2, title: Product 1}] await db.close(); } ``` -------------------------------- ### Activate sqflite_common_ffi_web Globally Source: https://pub.dev/packages/sqflite_common_ffi_web/install Use this command to install the package globally if you intend to use its executables. ```bash dart pub global activate sqflite_common_ffi_web ``` -------------------------------- ### Get Web Options for Database Factories Source: https://pub.dev/packages/sqflite_common_ffi_web/example Retrieves web-specific options for various database factories. This is useful for understanding the configuration of different web worker setups. ```dart for (var factory in [ databaseFactoryWebNoWebWorkerLocal, databaseFactoryWebPrebuilt, databaseFactoryWebLocal, databaseFactoryWebBasicWorkerLocal, ]) { try { write('factory: ${_tags[factory]}'); var options = await factory.getWebOptions(); write(const JsonEncoder.withIndent(' ').convert(options.toMap())); } catch (e) { write('exception $e'); } } ``` -------------------------------- ### sqflite FFI application example with path_provider Source: https://pub.dev/packages/sqflite_common_ffi Shows how to open a database at a specific path using path_provider, create a table, insert data, query it, and close the database. ```dart import 'dart:io' as io; import 'package:path/path.dart' as p; import 'package:sqflite_common/sqlite_api.dart'; import 'package:sqflite_common_ffi/sqflite_ffi.dart'; import 'package:path_provider/path_provider.dart'; Future main() async { // Init ffi loader if needed. sqfliteFfiInit(); var databaseFactory = databaseFactoryFfi; final io.Directory appDocumentsDir = await getApplicationDocumentsDirectory(); //Create path for database String dbPath = p.join(appDocumentsDir.path, "databases", "myDb.db"); var db = await databaseFactory.openDatabase( dbPath, ); await db.execute(''' CREATE TABLE Product ( id INTEGER PRIMARY KEY, title TEXT ) '''); await db.insert('Product', {'title': 'Product 1'}); await db.insert('Product', {'title': 'Product 1'}); var result = await db.query('Product'); print(result); // prints [{id: 1, title: Product 1}, {id: 2, title: Product 1}] await db.close(); } ``` -------------------------------- ### Import sqflite_common_ffi_web Modules Source: https://pub.dev/packages/sqflite_common_ffi_web/install Import the setup and main sqflite_ffi_web modules into your Dart code to use the package's functionality. ```dart import 'package:sqflite_common_ffi_web/setup.dart'; import 'package:sqflite_common_ffi_web/sqflite_ffi_web.dart'; ``` -------------------------------- ### Simple sqflite FFI unit test example Source: https://pub.dev/packages/sqflite_common_ffi A basic unit test demonstrating opening an in-memory database and checking its version using sqflite_common_ffi. ```dart import 'package:test/test.dart'; import 'package:sqflite_common/sqlite_api.dart'; import 'package:sqflite_common_ffi/sqflite_ffi.dart'; void main() { // Init ffi loader if needed. sqfliteFfiInit(); test('simple sqflite example', () async { var db = await databaseFactoryFfi.openDatabase(inMemoryDatabasePath); expect(await db.getVersion(), 0); await db.close(); }); } ``` -------------------------------- ### Use FFI Web Factory for Database Operations Source: https://pub.dev/packages/sqflite_common_ffi_web In web applications, use the databaseFactoryFfiWeb to open and interact with SQLite databases. This example demonstrates opening a database and querying its SQLite version. ```dart // Use the ffi web factory in web apps (flutter or dart) var factory = databaseFactoryFfiWeb; var db = await factory.openDatabase('my_db.db'); var sqliteVersion = (await db.rawQuery('select sqlite_version()')).first.values.first; print(sqliteVersion); // should print 3.39.3 ``` -------------------------------- ### Get Value from Shared Worker Source: https://pub.dev/packages/sqflite_common_ffi_web/example Retrieves a value associated with a key from a shared worker context. Ensure the worker context is properly initialized. ```dart Future getTestValue(SqfliteFfiWebContext context) async { var response = await context.sendRawMessage([ commandVarGet, {'key': key}, ]) as Map; return (response['result'] as Map)['value'] as Object?; } ``` -------------------------------- ### Sqflite FFI Web Initialization and Options Source: https://pub.dev/packages/sqflite_common_ffi_web/example Demonstrates setting up Sqflite FFI for web, including options for shared workers and basic workers. This is typically done once at the application's entry point. ```dart import 'dart:async'; import 'dart:convert'; import 'dart:math'; import 'dart:typed_data'; import 'package:sqflite_common/sqlite_api.dart'; import 'package:sqflite_common/utils/utils.dart'; import 'package:sqflite_common_ffi_web/sqflite_ffi_web.dart'; import 'package:sqflite_common_ffi_web/src/database_factory.dart'; import 'package:sqflite_common_ffi_web/src/sw/constants.dart'; import 'package:sqflite_common_ffi_web/src/web/load_sqlite_web.dart' show SqfliteFfiWebContextExt; import 'package:web/web.dart' as web; import 'ui.dart'; /// quick test open for basic web worker (as we cannot have both shared and simple worker) var _useBasicWebWorker = false; // devWarning(true); var _debugVersion = 1; var _shc = '/_shc$_debugVersion'; var swOptions = SqfliteFfiWebOptions( sharedWorkerUri: Uri.parse('sw.dart.js'), // ignore: invalid_use_of_visible_for_testing_member forceAsBasicWorker: _useBasicWebWorker, ); var swBasicOptions = SqfliteFfiWebOptions( sharedWorkerUri: Uri.parse('sw.dart.js'), // ignore: invalid_use_of_visible_for_testing_member forceAsBasicWorker: true, ); Future incrementPrebuilt() async { await incrementSqfliteValueInDatabaseFactory( databaseFactoryWebPrebuilt, tag: 'prebuilt', ); } Future incrementWork() async { await incrementSqfliteValueInDatabaseFactory( databaseFactoryWebLocal, tag: 'work', ); } Future exceptionWork() async { try { var factory = databaseFactoryWebLocal; var db = await factory.openDatabase('test_missing'); await db.query('Test'); } catch (e) { write('exception $e'); } } class _Data { late Database db; } /// Out internal data. // ignore: library_private_types_in_public_api final _Data data = _Data(); /// Get the value field from a given id Future getValue(int id) async { return ((await data.db.query('Test', where: 'id = $id')).first)['value']; } /// insert the value field and return the id Future insertValue(dynamic value) async { return await data.db.insert('Test', {'value': value}); } /// insert the value field and return the id Future updateValue(int id, dynamic value) async { return await data.db.update('Test', {'value': value}, where: 'id = $id'); } Future bigInt() async { try { var factory = databaseFactoryWebLocal; var db = data.db = await factory.openDatabase('test_big_int'); await db.execute( 'CREATE TABLE IF NOT EXISTS Test (id INTEGER PRIMARY KEY AUTOINCREMENT, value INTEGER)', ); await db.query('Test'); } catch (e) { write('exception $e'); } var id = await insertValue(-1); assert(await getValue(id) == -1); // less than 32 bits id = await insertValue(pow(2, 31)); assert(await getValue(id) == pow(2, 31)); // more than 32 bits id = await insertValue(pow(2, 33)); //devPrint('2^33: ${await getValue(id)}'); assert(await getValue(id) == pow(2, 33)); id = await insertValue(pow(2, 62)); //devPrint('2^62: ${pow(2, 62)} ${await getValue(id)}'); assert(await getValue(id) == pow(2, 62)); /* var value = pow(2, 63).round() - 1; id = await insertValue(value); //devPrint('${value} ${await getValue(id)}'); expect(await getValue(id), value, reason: '$value ${await getValue(id)}'); value = -(pow(2, 63)).round(); id = await insertValue(value); //devPrint('${value} ${await getValue(id)}'); expect(await getValue(id), value, reason: '$value ${await getValue(id)}'); */ } Future incrementNoWebWorker() async { await incrementSqfliteValueInDatabaseFactory( databaseFactoryWebNoWebWorkerLocal, tag: 'ui', ); } Future main() async { // sqliteFfiWebDebugWebWorker = true; initUi(); write('$_shc running $_debugVersion'); // devWarning(incrementVarInSharedWorker()); // await devWarning(bigInt()); // await devWarning(exceptionWork()); // await devWarning(incrementWork()); // await devWarning(incrementPrebuilt()); // await incrementVarInServiceWorker(); // await incrementSqfliteValueInDatabaseFactory( // databaseFactoryWebNoWebWorkerLocal); // await incrementSqfliteValueInDatabaseFactory(databaseFactoryWebLocal); // await devWarning( // incrementSqfliteValueInDatabaseFactory(databaseFactoryWebPrebuilt)); } var _webContextRegisterAndReady = sqfliteFfiWebStartSharedWorker(swOptions); var _webBasicContextRegisterAndReady = sqfliteFfiWebStartSharedWorker( swBasicOptions, ); Future sharedWorkerRegisterAndReady() async => (await _webContextRegisterAndReady).sharedWorker!; Future webContextRegisterAndReady() async => (await _webContextRegisterAndReady); Future webBasicContextRegisterAndReady() async => (await _webBasicContextRegisterAndReady); var databaseFactoryWebPrebuilt = databaseFactoryFfiWeb; ``` -------------------------------- ### Add sqflite_common_ffi_web Dependency Source: https://pub.dev/packages/sqflite_common_ffi_web Add the sqflite_common_ffi_web package to your project's dependencies in pubspec.yaml. ```yaml dependencies: sqflite_common_ffi_web: ``` -------------------------------- ### BSD 2-Clause License Source: https://pub.dev/packages/sqflite_common_ffi_web/license This is the standard BSD 2-Clause License text for the sqflite_common_ffi_web package. It specifies the conditions for redistribution and use, and includes a disclaimer of liability. ```text BSD 2-Clause License Copyright (c) 2022, Alexandre Roux Tekartik All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ``` -------------------------------- ### Add sqflite_common_ffi_web to Flutter Project Source: https://pub.dev/packages/sqflite_common_ffi_web/install Add the package as a dependency to your Flutter project using the flutter pub add command. ```bash $ flutter pub add sqflite_common_ffi_web ``` -------------------------------- ### Use sqlite3 v2 with sqflite_common_ffi Source: https://pub.dev/packages/sqflite_common_ffi Specify version constraints for sqlite3 and sqflite_common_ffi when using sqlite3 v2. ```yaml dependencives: sqlite3: ^2.9.4 sqflite_common_ffi: ^2.3.7 ``` -------------------------------- ### Use sqlite3 v3 with sqflite_common_ffi Source: https://pub.dev/packages/sqflite_common_ffi Specify version constraints for sqlite3 and sqflite_common_ffi when using sqlite3 v3. ```yaml dependencives: sqlite3: ^3.0.0 sqflite_common_ffi: ^2.4.0 ``` -------------------------------- ### Add sqflite_common_ffi as a dev dependency Source: https://pub.dev/packages/sqflite_common_ffi Include sqflite_common_ffi in your dev_dependencies to use it for unit testing. ```yaml dev_dependencies: sqflite_common_ffi: ``` -------------------------------- ### Add sqflite_common_ffi_web to Dart Project Source: https://pub.dev/packages/sqflite_common_ffi_web/install Add the package as a dependency to your Dart project using the dart pub add command. ```bash $ dart pub add sqflite_common_ffi_web ``` -------------------------------- ### BSD 2-Clause License Source: https://pub.dev/packages/sqflite_common_ffi/license This is the BSD 2-Clause License text for the sqflite_common_ffi package. It grants broad permissions for use, modification, and distribution, with certain conditions regarding copyright notices and disclaimers. ```text BSD 2-Clause License Copyright (c) 2019, Alexandre Roux Tekartik All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ``` -------------------------------- ### Adapt Existing sqflite App for Web Source: https://pub.dev/packages/sqflite_common_ffi_web For existing iOS/Android applications, conditionally change the default database factory to databaseFactoryFfiWeb when running on the web. This ensures compatibility across platforms. ```dart import 'package:sqflite_common_ffi_web/sqflite_ffi_web.dart'; import 'package:sqflite/sqflite.dart'; var path = '/my/db/path'; if (kIsWeb) { // Change default factory on the web databaseFactory = databaseFactoryFfiWeb; path = 'my_web_web.db'; } // open the database var db = openDatabase(path); ``` -------------------------------- ### Add sqflite_common_ffi to Flutter Project Source: https://pub.dev/packages/sqflite_common_ffi/install Use this command to add the package as a dependency in your Flutter project. ```bash $ flutter pub add sqflite_common_ffi ``` -------------------------------- ### Dependency Constraints for sqflite3 v2 and sqlite3 Source: https://pub.dev/packages/sqflite_common_ffi/changelog Specifies the version constraints for both sqflite_common_ffi and sqlite3 when using sqlite3 v2. This is an alternative to the previous constraint. ```yaml sqflite_common_ffi: ^2.3.7 sqlite3: ^2.9.4 ``` -------------------------------- ### Read and Write Database Bytes Source: https://pub.dev/packages/sqflite_common_ffi_web/example Writes a specified number of bytes to a database file and then reads them back. Useful for testing database file handling capabilities. ```dart Future readWriteDatabase(DatabaseFactory factory, int size) async { try { var path = 'read_write.db'; var bytes = Uint8List.fromList(List.generate(size, (index) => index % 256)); await factory.writeDatabaseBytes(path, bytes); var readBytes = await factory.readDatabaseBytes(path); write('wrote ${bytes.length}, read ${readBytes.length}'); } catch (e) { write('Exception $e'); } } ``` -------------------------------- ### Add sqflite_common_ffi to Dart Project Source: https://pub.dev/packages/sqflite_common_ffi/install Use this command to add the package as a dependency in your Dart project. ```bash $ dart pub add sqflite_common_ffi ``` -------------------------------- ### Import sqflite_common_ffi in Dart Code Source: https://pub.dev/packages/sqflite_common_ffi/install Import the package into your Dart files to use its functionalities. ```dart import 'package:sqflite_common_ffi/sqflite_ffi.dart'; ``` -------------------------------- ### FFI Process Exited Successfully Source: https://pub.dev/packages/sqflite_common_ffi_web/score/log.txt Indicates that the FFI process completed without errors. This is a status message. ```text ### Execution of process exited 0 STOPPED: 2026-06-13T00:45:16.569607Z ``` -------------------------------- ### Increment Sqflite Value in Database Factory Source: https://pub.dev/packages/sqflite_common_ffi_web/example Opens a database, creates a 'Test' table if it doesn't exist, and increments a 'value' for 'id = 1'. This function is designed to be used with a DatabaseFactory. ```dart Future incrementSqfliteValueInDatabaseFactory( DatabaseFactory factory, { String? tag, }) async { tag ??= 'db'; try { write('/$tag accessing db...'); // await factory.debugSetLogLevel(sqfliteLogLevelVerbose); var db = await factory.openDatabase( 'test.db', options: OpenDatabaseOptions( version: 1, onCreate: (db, version) async { await db.execute( 'CREATE TABLE Test(id INTEGER PRIMARY KEY, value INTEGER)', ); }, ), ); Future readValue() async { var value = firstIntValue( await db.query('Test', columns: ['value'], where: 'id = 1'), ) ?? 0; return value; } var value = await readValue(); write('/$tag read before $value'); await db.insert('Test', { 'id': 1, 'value': (value ?? 0) + 1, }, conflictAlgorithm: ConflictAlgorithm.replace); value = await readValue() ?? 0; write('/$tag read after $value'); } catch (e) { write('/$tag error: $e'); rethrow; } } ``` -------------------------------- ### Dependency Constraint for sqflite3 v2 Support Source: https://pub.dev/packages/sqflite_common_ffi/changelog Specifies the version constraint for sqflite_common_ffi when using sqlite3 v2. This ensures compatibility with older sqlite3 versions. ```yaml sqflite_common_ffi: ">=2.3.7 <2.4.0" ``` -------------------------------- ### Process Exited Successfully Source: https://pub.dev/packages/sqflite_common_ffi/score/log.txt Indicates that a process has completed its execution with an exit code of 0, signifying success. This is a common status indicator for background operations or external command executions. ```text STOPPED: 2026-06-13T00:46:21.250739Z ``` -------------------------------- ### Set Value in Shared Worker Source: https://pub.dev/packages/sqflite_common_ffi_web/example Sets a value associated with a key in a shared worker context. This operation is asynchronous. ```dart Future setTestValue(SqfliteFfiWebContext context, Object? value) async { await context.sendRawMessage([ commandVarSet, {'key': key, 'value': value}, ]); } ``` -------------------------------- ### Increment Variable in Basic Worker Source: https://pub.dev/packages/sqflite_common_ffi_web/example Increments an integer variable stored in a basic worker. Similar to the shared worker increment, it reads, increments, and writes back the value, defaulting to 0 for non-integer types. ```dart Future incrementVarInBasicWorker() async { var context = await webBasicContextRegisterAndReady(); write('basic worker ready'); var value = await getTestValue(context); write('var before $value'); if (value is! int) { value = 0; } await setTestValue(context, value + 1); value = await getTestValue(context); write('var after $value'); } ``` -------------------------------- ### Increment Variable in Shared Worker Source: https://pub.dev/packages/sqflite_common_ffi_web/example Increments an integer variable stored in a shared worker. It reads the current value, increments it, and writes it back. Handles non-integer initial values by defaulting to 0. ```dart Future incrementVarInSharedWorker() async { var context = await webContextRegisterAndReady(); write('shared worker ready'); var value = await getTestValue(context); write('var before $value'); if (value is! int) { value = 0; } await setTestValue(context, value + 1); value = await getTestValue(context); write('var after $value'); } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.