How can I swap the SQLite file in OrmLite? - android

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.

Related

Room database storage structure

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

Unidirectionally sync Android SQLite database from external MySQL database

I have a large local SQLite database in my app. I need to keep it up to date with a MySQL database. I was thinking of generating some kind of XML file with the changes and then having the client download that and use it to update the local database. Records are created and deleted with each update. Is there a better way to do this? How should I go about this? I am really open to taking any route at this point.
This is almost a year old, but this is how I did it in case anyone stumbles upon this.
-Have a MySQL database that holds all the changes
-When a change is added, it is generated with a Unix timestamp
-A column exists as a boolean for "deleted"
-PHP page takes timestamp as a URL variable
-returns all changes after that timestamp as a .CSV
-Application downloads .CSV, parses it, and updates its local SQLite database
Simple as that, works great.

Updgrading database downloading it from a server

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.

One time insert, multiple reads into SQLite Android app

I know there are similar responses so I am going to make this very succinct. I am planning on developing an app which has 18 chapters and each chapter has 30 or 40 hymns. Now, Im planning on using an SQLite command, insert each hymn individually but after the insert, and after the APK file is generated, would the data on the database still be present? Or Does it need to inserted in on each install? What are my options?
If you use sqlite database for you app...every time the app is installed.. new database will be created(of course the old one will be deleted).. and so the hymns will be inserted on each install..(but once you install.. on running your app wont create new database and insertions..).. hope this is clear..
I am not Clear with your question..but if you r inserting data through your code..than on each installation your records would be inserted once..if you provide condition to do so for only once.
You have to code the insertion of the hymns at the start/launch of the application, so that the database is ready for retrieval for the application. But the next time, the application is started, check whether your database exists and has the hyms (size of database), if yes, dont populate the database again, if no, populate it. I hope you want to read from a file/array and insert the records to the database.Once you insert the records to the database, they are available for the reference until the application is uninstalled or the database is re-created. Sqlite database is a persistent storage. Now, Im planning on using an SQLite command, insert each hymn individually but after the insert, and after the APK file is generated, would the data on the database still be present? Yes it would be present. Once the application is installed, the code of database would be executed and the database would be created.
My suggestion to you is use the XML parsing to show the Hymens in place of sqllite. Simply create the xml file with the hymen tag then get the tag and show the data on screen.

Android SQLite reading

I want to only read data from the SQLite database. When I am creating database and reading it it is working but I have already a database created and I want to read data from this database.
I am pushing the database to the sdcard and trying to run the application but it is not reading form the database. I want to know that if install this .apk file in device then my database will also shift to the device or not.
Common practice is to store initial data on assets/raw folders of application resources. Then during 1st run just create DB using SQL scripts like:
create table if not exist
Fill DB with initial data - and here you're.

Categories

Resources