React Native - SQLite Can not find pre-populated database file
Asked Answered
C

4

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
Callup answered 12/2, 2019 at 9:48 Comment(2)
Did you find the answer? I am also facing the same issueAleut
@TejasPatel i found answer myself. Please see my answer.Callup
C
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"});
Callup answered 28/2, 2019 at 9:50 Comment(4)
i have some issues with the sqlite in react native, can you help me please ?Tjirebon
That does not work for me :(. So sadStrain
if i save my db in the app cache then how can access them?Dimercaprol
I have the same issue. can you help me please?Ligation
S
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.

Seersucker answered 15/8, 2019 at 8:11 Comment(1)
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?Ligation
C
0

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

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

Crispa answered 20/10, 2024 at 18:53 Comment(0)
A
-1

If you are using defaults then, try:

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

this should work.

Aleut answered 28/2, 2019 at 5:52 Comment(1)
i have some issues with the sqlite in react native, can you help me please ?Tjirebon

© 2022 - 2025 — McMap. All rights reserved.