Tutorial :How to update a database used as a source of data by an Android app



Question:

I have an app which uses a large amount of data which has been compiled outside the app (on my main PC). The app is for my personal use so there are no complications with having to distribute data updates to other users. However, I am currently following a very convoluted and time-consuming procedure each time I want to update the data, and I wonder if anyone can suggest any ways to streamline it.

The procedure I follow whenever I want to add new data is as follows:

  • I enter the new data into a csv file which I maintain as the source of the relevant table in the database

  • I use SQLite Database Browser to import the data into an existing SQLite database. (This program does not seem to have the ability to append imported data into an existing table, so whenever a table needs updating I have to delete the existing table, then import data from the csv file into a new table, then manually edit the data types for all the fields in the table.)

  • I drag the icon for the database file onto the 'assets' folder of my project in Eclipse.

  • I export the project from Eclipse as an apk file.

  • I copy the apk file to my phone

  • (using Astro File Manager) I uninstall the old version of the app and install the new apk.

  • when the app is run, code based on the example set out here copies the data from the 'assets' folder into the app's data folder; this means that each byte of data takes up two bytes in the phone's internal memory; at present this is not a problem, but could be as the volume of data grows; I wonder if there is a more memory-efficient method?

I should be very grateful for any guidance.


Solution:1

I have found that I can have the SQLite database on the SD card, which simplifies things for me considerably (and reduces the amount of internal memory occupied by my app.)

The first clue was this blog which told me that it is possible (contrary to the impression I had gained from the Android documentation and from my earlier googling) for an app to refer to a database on the SD card.

I then found this page here on Stack Overflow which points out how simple the code can be.

With the help of the advice on these pages, I now have much simpler DBHelper code in my app where the openDataBase() method simply reads as follows:

public void openDataBase() throws SQLException{
File dbfile = new File(DB_PATH + DB_NAME); myDataBase = SQLiteDatabase.openOrCreateDatabase(dbfile, null); }

There is no longer any need to include the database in my project's "assets", nor therefore to include it in the apk file. And the great advantage is that the database can be updated without having to go anywhere near Eclipse, let alone reinstalling the app.


Solution:2

I don't really know about the database but I do know that if in eclipse you run the app on your phone it will automatically install it. So instead of running it in the emulator when you press the play button, run it on your phone and many of those steps will be eliminated.

To Wipe Database Clean on startup on the emulator:
Click the down arrow next to the play button in eclipse
Select Run Configurations
Click the Target Tab
Check the "Wipe User Data" check box alt text http://www.freeimagehosting.net/uploads/fe7250b026.png


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »