andpor / react-native-sqlite-storage

Full featured SQLite3 Native Plugin for React Native (Android and iOS)
MIT License
2.76k stars 520 forks source link

Pre-populated database not imported properly #468

Closed Stophface closed 2 years ago

Stophface commented 3 years ago

Expected Behavior

I expect the database to be moved and have the table in it

Current Behavior

I move the database and try to SELECT something from the table, but it returns that the table does not exist.

Possible Solution

I suspect the moving of the database has not worked. SQLite rather creates a completly new database on SQLite.openDatabase({ name: 'foo', location: 'default' }..., and of course, that has no table: bar yet.

Steps to Reproduce (for bugs)

I have a pre-populated database named foo.db located in www/sqlite/. I move this database to the internal storage like this

try {
    await SQLite.openDatabase({ name: foo, createFromLocation: '~/sqlite/foo.db' });
    console.log('Database successfully moved.');
} catch (error) {
    console.log('Error on moving database. Error: ', error);
}

This logs me Database successfully moved. so I suspect it worked. Then I try to open the database like this.

SQLite.openDatabase({ name: 'foo', location: 'default' },
    db => {
        console.log('Successfully opened Database:', db);
        db.transaction(tx => {
            tx.executeSql('SELECT * FROM bar', [], (_, { rows }) =>
                console.log(JSON.stringify(rows))
            )
        });
    },
    error => {
        console.log('Error on opening database locations.db: ', error);
    });

This logs me Successfully opened Database with this

{
    "dbname": "locations", 
    "openError": [Function anonymous], 
    "openSuccess": [Function anonymous], 
    "openargs": {
        "dblocation": "nosync", 
        "location": "default", 
        "name": "foo"
        }
}

That looks like a Database Object to me. But then returns me on tx.executeSql('SELECT * FROM bar'... this

Possible Unhandled Promise Rejection (id: 0):

Object {
  "code": 5,
  "message": "no such table: bar",
}

Your Environment

adamgins commented 3 years ago

@Stophface, did you make any progress with this?

huzeyfetas commented 3 years ago

The same issue...

I have the same problem and have been looking for a solution for about a week. I'm downloading a database from the web service, for example, let's say hello.db. After moving it to the databases folder on the android side very comfortably, I took care of all my problems with the explanations in the documentation, it is superb, but I faced a complete disaster on the ios side. There is neither a databases folder that needs to be moved nor the opendatabase command can not read the database file I have. object {code 5, message: no such table table_name}

edit: one week later I found solution. "https://github.com/andpor/react-native-sqlite-storage/issues/164" mike-niemand commented on 17 May 2017 answer helped me. Mike says db and location don't work with the same name, but for me it's just the opposite. It worked when db and location have the same name.

infinitbility commented 3 years ago

When you want pre-populate database then every time you have to create a new database file and delete previous database or re build application.

check out below documentation

https://infinitbility.com/react-native-sqlite-storage-examples-of-query

osupa commented 2 years ago

For me, there was a little bit of subtlety getting this to work on iOS as I kept experiencing the infamous "no such table found" error. I assumed that all I had to do was create a www directory under my project through the file system and it would all work. I created the directory and placed my sqlite db there and verified it in the project viewer in Xcode. However, it did not copy my database into /Users..../Library/Developer/CoreSimulator/Devices/MyApp.app/HASH-ID/Library/LocalDatabase/my-sqlite-db.db

I only discovered the source of this issue when I used Xcode to step through the source code of react-native-sqlite-storage/platforms/ios/SQLite.m around line #159 which starts with

RCT_EXPORT_METHOD(open: (NSDictionary *) options success:(RCTResponseSenderBlock)success error:(RCTResponseSenderBlock)error)

"Step 3 - Add file to project in XCode, right click on the main folder and select Add Files to 'your project name"

in https://github.com/andpor/react-native-sqlite-storage is very important. Use Xcode to add the folder.

Good luck. Osupa.

Stophface commented 2 years ago

Following SnehalAgrawal comment in this Issue solved my Problem on iOS and Android https://github.com/andpor/react-native-sqlite-storage/issues/164