How to get the path of sqlite db in react native?

Viewed 3259

I am using the sqlite plugin in react native and want to upload the db in react native.

But I am getting stuck on how to get the path of the sqlite db

Any help?

3 Answers

On android it seems to be at /data/data/package_name/databases/

Here's how to use React-native sqlite:

  1. npm install --save react-native-sqlite-storage
  2. ** React Native 0.60 and above ** Run cd ios && pod install && cd ... Linking is not required in React Native 0.60 and above
  3. Add var SQLite = require('react-native-sqlite-storage') to your index.ios.js enter image description here
  4. Write application JavaScript code using the SQLite plugin
errorCB(err) {
  console.log("SQL Error: " + err);
},

successCB() {
  console.log("SQL executed fine");
},

openCB() {
  console.log("Database OPENED");
},

var db = SQLite.openDatabase("test.db", "1.0", "Test Database", 200000, openCB, errorCB);
db.transaction((tx) => {
  tx.executeSql('SELECT * FROM Employees a, Departments b WHERE a.department = b.department_id', [], (tx, results) => {
      console.log("Query completed");

      // Get rows with Web SQL Database spec compliance.

      var len = results.rows.length;
      for (let i = 0; i < len; i++) {
        let row = results.rows.item(i);
        console.log(`Employee name: ${row.name}, Dept Name: ${row.deptName}`);
      }

      // Alternatively, you can use the non-standard raw method.

      /*
        let rows = results.rows.raw(); // shallow copy of rows Array

        rows.map(row => console.log(`Employee name: ${row.name}, Dept Name: ${row.deptName}`));
      */
    });
});

NOTE: You must create a DB file for SQLite that uses the .db extension to use with React Native. so you can make it using sqlitbrowser.

reference document:

Where as on Android the location of the database file is fixed, there are three choices of where the database file can be located on iOS. The 'location' parameter you provide to openDatabase call indicated where you would like the file to be created. This parameter is neglected on Android.

where the location option may be set to one of the following choices:

  • default: Library/LocalDatabase subdirectory - NOT visible to iTunes and NOT backed up by iCloud
  • Library: Library subdirectory - backed up by iCloud, NOT visible to iTunes
  • Documents: Documents subdirectory - visible to iTunes and backed up by iCloud
  • Shared: app group's shared container - see next section

In Android studio When Launch succeeded: In Device File Explorer> data> data> com.nameofyourapp> databases

You can also click on the bottom tab of android studio 'Database inspector' to see the database changes in real time. :)

Related