Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open prepopulated SQLite data base in react-native, where to put database?

I am following instructions on importing existing db to my App (for IOS):https://www.npmjs.com/package/react-native-sqlite

Have created www folder in myProject directory, put there myDataBase.
Added folder to xcode.

this is my src code to open it and make a query :

import SQLite from 'react-native-sqlite-storage'

function errorCB(err) {
  console.log("SQL Error: " + err);
}

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

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

console.log('database.js')

var db = null;

export function openDB() {
// var db = SQLite.openDatabase("test.db", "1.0", "Test Database", 200000, openCB, errorCB);
db = SQLite.openDatabase({name : "words", createFromLocation : 1}, openCB,errorCB);

}

export function getWord(str) {
  db.transaction((tx) => {
    tx.executeSql("SELECT * FROM words", [], (tx, results) => {
        console.log("Query completed");

        // Get rows with Web SQL Database spec compliance.

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

        // 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}`));
        */
      });
  });
}

I am getting:

Built path to pre-populated DB asset from app bundle www subdirectory: /Users/mac/Library/Developer/CoreSimulator/Devices/06420F74-0E1C-47C1-BCAC-5D3574577349/data/Containers/Bundle/Application/75EE8E9A-276F-402F-982A-DBF30DE80802/MyApp.app/www/words
RCTLog.js:48 target database location: nosync
RCTLog.js:48 Opening db in mode READ_WRITE, full path: /Users/mac/Library/Developer/CoreSimulator/Devices/06420F74-0E1C-47C1-BCAC-5D3574577349/data/Containers/Data/Application/67D2451F-3D72-4B82-AC90-AD6DB9A82566/Library/LocalDatabase/words

Database opened
RCTLog.js:48 Good news: SQLite is thread safe!
dataBase.js:13 Database OPENED
RCTLog.js:48 open cb finished ok
sqlite.core.js:475 Error handler not provided:  {message: "no such table: words", code: 5}
sqlite.core.js:572 warning - exception while invoking a callback: {"code":5}

Don't know what is the reason of this error?
I have checked in DB browser for SQLite that DB is correct and table 'words' exists in it and have rows in it.
I have tried different names for db file: 'words', 'words.db', 'words.sqlite' nothing helps.

I am running my app from console as :

react-native run-ios
like image 617
spin_eight Avatar asked Feb 28 '18 16:02

spin_eight


People also ask

Where should I put SQLite database?

The SQLite DLL file can be placed on Windows in C:WINDOWSsystem32 folder if needed to manage your database files.

How do I add a database to React Native?

To install the "app" module, view the Getting Started documentation. # Install & setup the app module yarn add @react-native-firebase/app # Install the database module yarn add @react-native-firebase/database # If you're developing your app using iOS, run this command cd ios/ && pod install && cd ..

How do I find the path of SQLite database in React Native?

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.


1 Answers

SQLite.openDatabase({name:"testDB.sqlite3", createFromLocation:1,location:'Library'})

This solved my problem. testDB.sqlite3 file size is 24 MB. testDB.sqlite and testDB.db not working, but testDB.sqlite3 is working.

like image 127
H.yum Avatar answered Oct 04 '22 21:10

H.yum