andpor / react-native-sqlite-storage

Full featured SQLite3 Native Plugin for React Native (Android and iOS)
MIT License
2.76k stars 522 forks source link

Question about opening an existing database #18

Closed ipetepete closed 8 years ago

ipetepete commented 8 years ago

I have a pre-filled sqlite database, what steps are required to get this to open the provided file vs creating a new one?

my db file is called trivia.sqlite I'm using this to open it:

this.db = sqlite.openDatabase("trivia.sqlite", "1.0", "Trivia DB", 200000, this.openCB, this.errorCB);

It opens fine but when I try to get some data out it says: code 5: 'no such table'

andpor commented 8 years ago

Doing this is quite trivial. I will post full instructions on GIT later but will try to quickly explain how to here.

  1. create a folder called www in your parent project directory - I created one at the same level where other src files where.
  2. put your prepopulated db file into this www directory. Give it a name that matches the db name you provide to open call in your code. example 'testDB'
  3. add this www directory to your project, make sure you have 'Create Folder References' checked.
  4. in your JavaScript code, use a slightly different format of params to open call: openDatabase({name : "testDB", createFromLocation : 1}, okCallback,errorCallback)

adding createFromLocation makes the plugin read your file as template..

That should do it

Would you confirm this works for you and I will close the issue?

ipetepete commented 8 years ago

Sorry this didn't work. I created a new folder 'storage' added it to the project via 'Create Folder References'

One of the log outputs says this:

using db location: docs

I can try and manipulate the file location within the app and try and get it into the Documents directory to see if that works, but it seems like it is an unnecessary step.

andpor commented 8 years ago

You cannot have a 'storage' folder - it has to be named 'www'. database file should be a child of this folder and must be named exactly the same as the name of db in opendatabase call.

If it doesn't work after you make this adjustment, can you include opendb call you use an provide a screenshot of parent project structure showing how the file is included.

ipetepete commented 8 years ago

YAY! That worked. Sorry about that, www seems like such an unrelated name for a directory, but it works, thats all that matters.

Thank you!

andpor commented 8 years ago

remember Cordova heritage of this plugin...glad it worked. Maybe I will change the name to something more intuitive in the future..

NiteshDhakal commented 6 years ago

image

var db = SQLite.openDatabase({name: "roommate", createFromLocation : 1});

and i didn't got data, :(

andpor commented 6 years ago

"roommate.db" ??