Moor: how to test moor database on windows (sqlite dependency is needed)

Created on 29 Feb 2020  路  3Comments  路  Source: simolus3/moor

this is the error you get when trying to test your database on windows because sqlite is needed as a dependency on windows. i have tried adding it but failed. it would be great if someone could help with this since alot of developers are windows users

dart:ffi                                                       new DynamicLibrary.open
package:moor_ffi/src/load_library.dart 34:27                   _defaultOpen
package:moor_ffi/src/load_library.dart 80:12                   OpenDynamicLibrary.openSqlite
package:moor_ffi/src/bindings/bindings.dart 92:19              new _SQLiteBindings
package:moor_ffi/src/bindings/bindings.dart 184:53             bindings
package:moor_ffi/src/impl/database.dart 40:9                   new Database.open
package:moor_ffi/src/impl/database.dart 32:41                  new Database.memory
package:moor_ffi/src/vm_database.dart 41:22                    _VmDelegate.open
package:moor/src/runtime/executor/helpers/engines.dart 246:22  DelegatedDatabase.ensureOpen.<fn>

Invalid argument(s): Failed to load dynamic library (126)
backend-ffi question

All 3 comments

If you need a setup for testing, you can just download the dll file. You'll find the dll under "Precompiled Binaries for Windows". If you extract that dll file into somewhere in your PATH, DynamicLibrary.open should be able to pick it up.

If you use chocolatey, choco install sqlite should also do the trick.

here is the code that finally worked for me after installing sqlite by chocolatey
(install chocolatey package manager if you haven't already)

so first -> choco install sqlite

and here is the code :

my database class :

class MoorDb extends _$MoorDb {
  MoorDb(QueryExecutor qe) : super(qe );

  @override
  MigrationStrategy get migration => MigrationStrategy(
        onCreate: (migrator) async {
          await migrator.createAll();
        },
        onUpgrade: (migrator, from, to) async {
          await migrator.createAll();
        },
      );

  @override
  int get schemaVersion => 1;
}

and here is my test file :

import 'package:flutter_test/flutter_test.dart';
import 'package:moor_ffi/moor_ffi.dart';
import 'package:smartschool/data/datasource.dart';
import 'package:smartschool/data/local/database.dart';
import 'package:smartschool/data/local/moor_database.dart';

void main() {
  MoorDb moorDb;


  //setup and tear down
  setUp(() {
    moorDb = MoorDb(VmDatabase.memory(logStatements: true));
  });

  tearDown(() {});

  test('simple test', () async {
    //not an actual unit test. only to check it's working
    await moorDb.into(moorDb.users).insert(User(id: 1, roll: 'teacher', mobile: '09117158746'));
    var users = await moorDb.select(moorDb.users).get();
  });
}

the dependencies i used for moor database :

dependencies:
  moor: ^2.4.1
  moor_ffi: ^0.4.0
  path_provider:
dev_dependencies:
  build_runner:
  moor_generator: ^2.4.0

I've copied the sqlite3.dll file into the root folder of my project, and it worked.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

stx picture stx  路  3Comments

felixjunghans picture felixjunghans  路  4Comments

jerryzhoujw picture jerryzhoujw  路  4Comments

omidraha picture omidraha  路  3Comments

KKRoko picture KKRoko  路  3Comments