react-nativereact-native-ioscordova-sqlite-storage

sqlite3 is not opening pre-populated database second time


I'm exploring sqlite 3 plugin and I found one weird issue which I didn't found through out web.

I'm working on android .So, in asset folder I have a database with some data. The database name was sqliteexample.db. Then I used below code to open database. And it was a success.

var SQLite = require('react-native-sqlite-storage');
var db = SQLite.openDatabase(
  {
    name: 'sqliteexample',
    createFromLocation: '1'
  },
  () => {
    console.log('db connection success');
  },
  (error) => {
    console.log('db connection error');
    console.log(error)
  },
);

Now, In order to check different scenario's I renamed the database file name to sqliteexample1.db. Then I changed it on code as below

{
    name: 'sqliteexample1',
    createFromLocation: '1'
  },

That's all, I started getting error cannot open database.

How internally It was accessing It ?
And If I skip createFromLocation: '1' will it create a new database ?

Kindly anyone help me on this.


Solution

  • very simple,

    if data file is named the same as the dbName

    SQLite.openDatabase({name : "sqliteexample", createFromLocation : 1}, okCallback,errorCallback);
    
    

    if data file is named different than dbName

    SQLite.openDatabase({name : "sqliteexample", createFromLocation : "~sqliteexample1.db"}, okCallback,errorCallback);
    
    

    if your folder is not in app bundle but in app sandbox

    SQLite.openDatabase({name : "sqliteexample", createFromLocation : "pathroot/data/mydbfile.sqlite"}, okCallback,errorCallback);
    
    

    The thing is file name and database name need not be same always.