1
votes

I have a Cordova mobile app and I let the user import a SQLite dababase from an external .db file. I'm using the cordova-sqlite-storage

https://github.com/litehelpers/Cordova-sqlite-storage

A naive solution is already implemented but it's risky. Basically the user selects an external .db file using a file picker which replaces the old .db file

window.plugins.sqlDB.copyDbFromStorage("mydb.db", 0, file, true, onSuccess);

where file is the uri of the new .db file.

It works but I'd like to check the .db file first, at least those two checks:

  • check if the .db file is actually a SQLite database
  • check if the new SQLite database has some table that must be there before importing

What I've tried

I tried to find a way to open the .db file from its original directory but it seems that the openDatabase() only opens databases stored in the special default location (which is, I guess, the internal private storage for the app)

openDatabase({name: 'test.db', location: 'default'});

other values for the location parameter are simply discouraged, they suggest to stick with default

1
Try to open it; try to read the table. - CL.
@CL I tried but if you look closely you can only call the open database like this: openDatabase({name: 'test.db', location: 'default'}); It doesn't let you specify a path - Gianluca Ghettini

1 Answers

2
votes

If you cannot (or do not want to) open the database at the original location, then you have to copy it to the internal private storage location with a different file name. (You can then later copy over the old file, or delete and rename the files.)