SQLite existing database

Hello

We have a largish SQL lite DB (5.2MB) which we need to use within our app, but pulling data doesnt seem to work - it always gets treated as a new DB.

I’ve tried putting it in an “SQLite” folder within the root of the app then calling:
const db = SQLite.openDatabase(‘nrsv.db’);

I’ve tried calling
const db = SQLite.openDatabase(Expo.FileSystem.documentDirectory + ‘SQLite/nrsv.db’);

I’ve tried making it into an asset… nothing seems to work!

Is there an example anywhere of using an existing database rather than creating a new one?

Thanks
Ryan

1 Like

What does “putting in an SQLite folder in the root of my app” mean? If you use the FileSystem API’s .downloadAsync method to download a file to documentDirectory/SQLite, you can open it. You can find an example in https://github.com/expo/test-suite/blob/master/tests/SQLite.js in the “should work with a downloaded db file” test.

Thanks for the reply…

“putting in an SQLite folder in the root of my app” meant literally that, putting the folder in an SQLite folder - having read through other comments that seemed to be what resolved it for others.

I looked at the .downloadAsync code but its unfortunately throwing an error for me:
“Unable to resolve module ./assets/db/nrsv.db from /Users/ryan/Apps/Lectionary/App.js: could not resolve /Users/ryan/Apps/Lectionary/assets/db/nrsv.db as a file or folder.”

The file exists in this location, and is a valid SQLite file - are you able to offer any guidance on what the error might be?

The problem could be that the .db extension is ignored by the React Native packager (aka Metro). In your project’s app.json file, insert as appropriate:

"expo": {
  "packagerOpts": {
    "assetExts": ["db"]
  }
}

Then restart exp or click Restart in XDE so the new changes are applied to the packager and try again.

1 Like

That seemed to help… in the end my code looked like:

	// load in db
	Expo.FileSystem.downloadAsync(
    	Expo.Asset.fromModule(require('./assets/db/NRSV.db')).uri,
		`${Expo.FileSystem.documentDirectory}SQLite/nrsv.db`
	);

A lot of the code samples (including the 2nd comment here) used Expo.FS rather than Expo.FileSystem - I found once I switched over to the latter it worked without issue…

It might be worth adding something on this to the SQLite documentation as it seems like a regular use case.

Hope this helps someone else.

9 Likes

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.