0
votes

I'm trying to connect an sqlite db using react-native-sqlite-storage, I followed the steps in github, it works fine on android but not on IOS, It looks like that it can not find the db file even I put it in the supposed folder "iOS/www", here is the open statement:

import {openDatabase} from 'react-native-sqlite-storage';
.....
const database = openDatabase({
      name: 'hebron_db.db',
      createFromLocation: '~hebron_db.db',
      location: 'default'
}, () => {
   console.log('db connection success')
}, () => {
   console.log('db connection error')
});

It always types "db connection success" even if I change the file name to be incorrect.

I use react native 0.62.2 Xcode: 11.5 npm: 6.13.4 macOS: Catalina 10.15.5

shouldn't it go to catch section if the file does not exist? any help please??

2

2 Answers

0
votes

Here is the connection string that works finally:

import {openDatabase} from 'react-native-sqlite-storage';
const database = openDatabase({name : "hebron_db", createFromLocation : 1}, null, null);
export default database;

may it help others.

0
votes

Add on your App.js file. By doing this you will create a global reference to db

import {openDatabase} from 'react-native-sqlite-storage';


global.db = openDatabase(
  {
    name: 'xxx.db',
    createFromLocation: 1,
  },
  () => {},
  error => {
    console.log('ERROR: ' + error);
  },
);