sqlitereact-native

React Native - SQLite Can not find pre-populated database file


I'm trying to use https://github.com/andpor/react-native-sqlite-storage for SQLite.

I made a sqlite database and it is pre-populated.

On my react-native project, I put players.db file to /www folder as documentation says.

But when I inspect from console opening database is failing. I can not open my pre-populated sqlite database.

I tried these options and all of them not working for me;

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

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

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

And console output is:

OPEN database: players
Running application "SampleApp" with appParams: {"rootTag":31}. __DEV__ === true, development-level warning are ON, performance optimizations are OFF
new transaction is waiting for open operation
OPEN database: players failed, aborting any pending transactions

Solution

  • Finally I found the answer.

    ProjectFolder/android/app/src/main/assets directory is same to /www folder. So don't put your database file to /www. Put it to /android/app/src/main/assets folder.

    Then you can use it like

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