2

I have an SQLite database which I created by using DB Browser, I putted it in assets file and I use SQLiteAssetHelper to access it. Why if I update any record using DB Browser It dose not change in my app ?!
Also If I execute insert query in my app, the new record does not appear in DB Browser.

forpas
  • 160,666
  • 10
  • 38
  • 76
Noor
  • 57
  • 1
  • 7

2 Answers2

2

You're dealing with 3 different databases:

  1. The database you create with DB Browser and is stored in a folder in your pc.

  2. The database in assets folder which is a copy of the database you created with DB Browser

  3. The database in your emulator/device that was created the 1st time you ran the app.
    This last database originally was a copy of the database you had in assets folder.

When you make changes to the database in case 1, these changes are not reflected to either the database in assets folder or in your emulator/device.
If you want these changes to be made to your emulator/device database, you must:

  • Delete the database from assets and copy there the changed DB Browser database.
  • Uninstall the app from the emulator/device so the database is deleted.
  • Run the app again, so the database is recreated in the emulator/device.

When you make changes to the emulator/device and you want to examine the database with DB Browser:

  • In Android Studio select View > Tool Windows > Device File Explorer
  • In the Device File Explorer window go to data/data/yourpackagename/databases folder and right click your database and click Save as.... Select the folder where you want the database to be saved and you're done.
    Now you can open the database with DB Browser.
forpas
  • 160,666
  • 10
  • 38
  • 76
  • I deleted the database from assets file, I removed the application from the device, I run the application on my device and it run with the old database, it is still connected with my application – Noor Dec 10 '18 at 14:58
  • Did you first copy to the assets folder the new db you created with DB Browser? – forpas Dec 10 '18 at 15:00
  • I copied the new database to assets file then I reinstall the application but same problem happened .. the old database is stuck into my app. – Noor Dec 10 '18 at 15:12
  • 1
    Is there another way to remove the old database instead of uninstall the app? – Noor Dec 10 '18 at 15:14
  • Delete it from Device File Explorer – forpas Dec 10 '18 at 15:16
  • there is nothing in file Explorer – Noor Dec 10 '18 at 15:27
  • I mean `data/data/yourpackagename/databases` folder in **Device File Explorer** in Android Studio. If there is nothing there then the db was deleted. – forpas Dec 10 '18 at 15:29
  • I restart my phone and yeees the new database it is work .. thank you very much – Noor Dec 10 '18 at 15:32
0

Because SQLiteAssetHelper does not copy the database from assets if the database file already exists in the target location.

If it's a development-time change in the database, you can simply uninstall your app to delete the old version of the database.

If it's a change to a released version of your application, you need to implement database versioning and update scripts as mentioned in the readme.

Also If I execute insert query in my app, the new record does not appear in DB Browser.

You're not inserting to the same database but a copy of it. You need to pull the database from your device and check from there. See e.g. android adb, retrieve database using run-as

laalto
  • 150,114
  • 66
  • 286
  • 303