In my app I'm using Room with a prepopulated database myDatabase.db which is then accessed using this code:
private val database = Room
.databaseBuilder(context.applicationContext, AppDatabase::class.java, dbName)
.createFromAsset("myDatabase.db")
.build()
The file myDatabase.db is stored in the /assets/ folder. Then I had to add some new rows in one on my tables in such database. After that, I got an updated myDatabase.db file, which I copied to the /assets/ folder replacing the old file. And here comes the problem: after installing the app, it still uses the old data. I did some searching but I only found results related to migrations. However, in my case, there is no change in the schema of the table, only some new rows are added. Furthermore, I don't understand why the app doesn't pick the new myDatabase.db file and still uses the old one.
The only solution to get the app to use the new file myDatabase.db was to uninstall the app and then install it again, but that's something I'd like to avoid. So, how can I change the file myDatabase.db in assets folder and make the app pick the data in the new file through Room?
Furthermore, I don't understand why the app doesn't pick the new myDatabase.db file and still uses the old one.
A Room database is persistent and lasts until it is deleted. createFromAsset will only be invoked when the database does not exist and hence why, under normal situations it will not replace the existing database.
A solution assuming that the App only reads the pre-populated daatbase is to:-
add either of the fallbackToDestructiveMigration or fallbackToDestructiveMigrationFrom
Increase the database version number
DO NOT have a Migration that covers the old to new version
The migration instead of failing will delete the database and then attempt to create it. In doing so it will create the database from the new asset.
Related
I am a beginner in android programming, I created an android app which uses an exist DB with DBFlow. At first time I inserted a temp data to database so I can test the app. When I finished building the app I inserted real data and copied the new database -which is similar to the old one except for the real data- to assets folder but I did not find the new data.
How can I reset the database or change the data.
After changing database you must remove previous version of app from your device and then install the app again. This should solve your problem.
Where does Room store the database and how can I force a rebuild of the DB? I've tried looking for the DB under:
data/data/com.me.myapp/No database directory here
data/user/0/com.me.myapp/No database directory here
I want to see exactly what data is in the database using SQLLite so I followed these directions "Access database in Android Studio" but I only see a cache and codecache directory stored there. No database directory.
The reason for wanting to see the DB is that I changed the model to add a few fields, but I can't figure out how to force Room to recreate and repopulate the DB with data. I added breakpoints inside my data generator class; however they don't seem to ever get hit.
if you change your entities structure (aka add fields) then in order not to loose data and get the fields added when user updates the current version of the app you need to implement migration . See the docs how to do it. So updating the app version will make your db "rebuild" and not lose data. When I just work with test version I delete app and build it on device again it'll implement the changes in db structure but you'll lose the data.
You should definitively see db files in the directories you named.
If you want some other method to debug a database you could use this library git link
I have an android application that relies on a sqlite database, and use OrmLite to access my DB.
Instead of OrmLite creating the tables I rely on downloading the database from a central server as the user will often want to "sync" things. Currently I don't have the fancy sync code written so the app replaces the db. The steps are:
1 Download the latest SQLite db file from the server, as a zip
2 Expand the file to produce a database.sqlite file in a temporary folder
3 Deletes the contents of a data folder, which contains the live database.sqlite file
4 Move the database.sqlite file from the temporary folder to the data folder.
The problem is that the new database file seems to get ignored and DAO queries I run return old data. The only way to show data from the new version of the DB is to restart the application.
To test things I created a table with a timestamp that records when the database was generated, each time you request a new copy of the sqlite db from the server this is updated. I have a fragment that displays this time so you know how fresh your data is. In the fragments onResume method I make a call to the DAO to get the timestamp and put value on screen. I've stepped through this and I see the call to the DAO but the value that comes back is from the old, now deleted, db. Restart the app and the correct value is shown.
So my question is, if I replace the underlying sqlite db file that stores my database, how can I tell ormlite to pick it up or refresh the connection or whatever it has to do???
I tried calling clearObjectCache on the DAO, made no difference.
I have Database in my application which is created by just coping the SQLite database file from the assets folder but now in the next release I want to update the database without losing the previous data.
Is there any Logic or way to copy the previous data and create a new database through SQLiteOpenHelper so that in near future I will not face this problem.
Thanks
Use this gist
In the OnUpgrade method, copy your previous data from the old database.
In the doUpgrade method, use your copied data to update the new database.
Previous data will be kept by default when users update to a new version. but you must keep in mind there will be new users never get a former version app. So you should add complete database in the assets folder.
I have an app that creates a database and do some stuff. I am wondering if i upload a new db to a server and download it to the exact folder where the older one exists it will be overwritten and i am good to go? Or there will be a problem. Assuming it has the same name, same column names, etc. Of course i am reffering to sqlite.
In Android, when performing a database update you should be using onUpgrade inside of the SQLiteOpenHelper. One way of doing this is to download text files that include the sql instructions needed to modify the current database or update rows with new data. The reason you have to do this is because Android will only create the database once. After the initial creation the call to onCreate for the database will not occur.