How to connect to an existing database

Hello. I have a problem with prepopulated db and expo. After unsuccessful search for a solution I have decided create this topic. I cant find where should I put my sqlite db file. I have a sqlite db file(db.db) in SQLite folder insite root of my project. After that I try to make a connection

const db = SQLite.openDatabase(FileSystem.documentDirectory + 'SQLite/db.db');

in db variable I recive WebSQLDatabase object with name property "file:///data/user/0/host.exp.exponent/files/ExperienceData/%2540galart%252Fjokes/SQLite/db.db" but when I make a sql request, I recive an error "unknown error (Sqlite code 14): Could not open database, (OS error - 2:No such file or directory)".

But if I make this Expo.FileSystem.getInfoAsync(FileSystem.documentDirectory + 'SQLite/db.db'), it showes me that the database exists

exists:true
isDirectory:false
modificationTime:1515117501
size:16384
uri:"file:///data/user/0/host.exp.exponent/files/ExperienceData/%2540galart%252Fjokes/SQLite/db.db"

What is the problem?

Hi galart! Sorry this is giving you trouble. Could you try opening the database using just the file name, e.g. const db = SQLite.openDatabase('db.db'); ? I believe the API already assumes the file is in the SQLite folder at root. Let us know if that solves the issue.

We’re having the same problem as well… 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

Hi @thoughtco and @galart - it looks like the way to do this is to bundle the sqlite DB as an asset, and then use FileSystem.downloadAsync to ‘download’ the DB from the assets directory. See this thread for more details: SQLite existing database

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