3

I have tried to open Sqlite database using react-native for android application but couldn't open using prepopulated database stored inside assets directory of the project.

I have removed location and createfromLocation from the openDataBase code databae was created in android device which would be empty without schema and data. I can populate data from the code but this doesn't make any sense. So What might be the mistake that I am making.Am I missing something here?

Code(App.js file):

 const db = SQLite.openDatabase(
      {
        name: 'Recipe.db',
        location: 'default',
        createFromLocation: '~Recipe.db',
      }

Log:

 OPEN database: Recipe.db
05-20 09:22:23.620  5975  6009 I ReactNativeJS: new transaction is waiting for open operation
05-20 09:22:23.629  5975  6009 I ReactNativeJS: OPEN database: Recipe.db failed, aborting any pending transactions
05-20 09:22:23.632  5975  6009 I ReactNativeJS: { [Error: Could not open database]
05-20 09:22:23.632  5975  6009 I ReactNativeJS:   code: 0,
05-20 09:22:23.632  5975  6009 I ReactNativeJS:   line: 100047,
05-20 09:22:23.632  5975  6009 I ReactNativeJS:   column: 27,

1 Answer 1

1

You can't open a SQLite DB for writing if it is stored in the Assets folder. Typically, on startup, an app will check to see if it has a SQLite database in its own /data/data/database folder. If not, it copies the database from the Assets folder to the /data/data/database folder, then opens that copy for writing.

Changes made to the database while in the /data/data/database folder will persist, even if the app is updated. But, it will be deleted if the user deletes the app. Then the app will have to do the database copy again.

Sign up to request clarification or add additional context in comments.

3 Comments

I am trying to use database for write operation only. So Do the above code copy the Recipe.db from assets directory to the /data/data/database so that I can perform read operation on Recipe database? I have deleted the databases from /data/data/database location and start the app but still got an issue.
You need to add code in your app, which before you execute any code to read or write to the database, you call a method to see if the database exists in the /data/data/database folder, and if so, Open it. If it does not exist, then copy the database from the Assets folder to the /data/data/database folder then Open it from there.
My be there was an issue in configuration. Issue got resolved with same code. Thanks for the help.

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.