5

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
2
  • Did you find the answer? I am also facing the same issue Commented Feb 28, 2019 at 4:47
  • @TejasPatel i found answer myself. Please see my answer. Commented Feb 28, 2019 at 9:50

4 Answers 4

13

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"});
Sign up to request clarification or add additional context in comments.

4 Comments

i have some issues with the sqlite in react native, can you help me please ?
That does not work for me :(. So sad
if i save my db in the app cache then how can access them?
I have the same issue. can you help me please?
4

In Android, place your database file in 'PROJECT\android\app\src\main\assets' directory. If your database filename is different (say, players.db VS example.db) then,

  1. Its location should be: PROJECT\android\app\src\main\assets\example.db
  2. Import using:

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

If your filename is same as the database (both named players.db) then

  1. Its location should be: PROJECT\android\app\src\main\assets\players.db
  2. Import using:

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

If you have already messed up with importing a pre-populated database, try clearing up the application data from the emulator/mobile phone and/or uninstalling the app completely before following the steps above.

1 Comment

I have changes the db location to be PROJECT\android\app\src\main\assets\players.db but I still not been able to read any of the tables. can you help me please?
0

For me it worked after I put my sqlite db file in this directory:

/android/app/src/main/assets/www

Comments

-1

If you are using defaults then, try:

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

this should work.

1 Comment

i have some issues with the sqlite in react native, can you help me please ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.