Moor: Provide better documentation on using Moor with existing databases

Created on 1 Aug 2019  路  4Comments  路  Source: simolus3/moor

I hope u will update documentation about embedding existing database assets into project.
Besides, I prefer using only moor things to do it.

_Originally posted by @KaungZawHtet in https://github.com/simolus3/moor/issues/26#issuecomment-517244493_

Most helpful comment

We now have some utils to make this easier on develop - the FlutterQueryExecutor takes a new parameter called creator which can create the database when it doesn't exist. Loading a database from an asset could be done with
https://github.com/simolus3/moor/blob/925f80aa9f35f00435d7cc8956c291df6abfceef/extras/integration_tests/flutter_db/lib/main.dart#L36-L44

All 4 comments

Any update on this issue ? I m trying to add existing database asset at assets/data/mydb.sqlite3 and failed. If we can do this, we can work with third party sqlite clients too leading to faster development.

Sorry for the delay, I'll try integrate proper support for that either this or next week to make this easier.

Until then, maybe you could use something like this setup to open the database, assuming your database file is called "app.db"

  1. Use getDatabasesPath from sqflite directly. Then, you could use join(dbPath, 'app.db') to get the location where moor expects the database file to be.
  2. Check if that file exists. If it doesn't, create it by copying the asset.
  3. Open your database using FlutterQueryExecutor.inDatabasePath('app.db')

It works now. Point was that task of loading database asset needs to be created first before moor's Database obj is created. Thx for the reply

We now have some utils to make this easier on develop - the FlutterQueryExecutor takes a new parameter called creator which can create the database when it doesn't exist. Loading a database from an asset could be done with
https://github.com/simolus3/moor/blob/925f80aa9f35f00435d7cc8956c291df6abfceef/extras/integration_tests/flutter_db/lib/main.dart#L36-L44

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tony123S picture tony123S  路  4Comments

simolus3 picture simolus3  路  4Comments

cadaniel picture cadaniel  路  4Comments

tony123S picture tony123S  路  4Comments

jerryzhoujw picture jerryzhoujw  路  4Comments