How to reset an Android SQLite database?
My application uses an SQLite database, and it's useful during development to be able to reset the database so I can have a fresh start. On iPhone, this can be accomplished by just removing the folder that gets generated in:
Library/Application Support/iPhone Simulator/4.1/Applications
How can I accomplish the same thing for Android?
7 Answers
-
@Mike the fastest way to clean up is to uninstall the app via adb.
adb -e uninstall APPID
For example to uninstall KitchenSink
adb -e uninstall com.appcelerator.titanium
-
The simplest way is to use the .remove() method on the database. Since the remove method does not take a name argument, you first open the database to identify it, then close and remove it:
var db = Ti.Database.open('myDB'); db.close(); db.remove();
The same code works for both iphone and android, and on the device or the simulator.
-
In addition to the methods described by the other answers, you can also use the adb tool to open an SQLite command prompt session. Then, you can issue SQL commands directly against your app's database. From there, you could truncate the table or something. See this thread for some how-to info.
In case someone is viewing this thread as a way to update a database as part of an app update, I wanted to point out that with John Welch's solution user data stored in the database would no longer be accessible. His method doesn't delete the old data. But your app's connection would point to the new database. If all you're doing is saving app preferences, you'd be better off using Titanium.App.Properties instead of a database.
If you do need to update the DB structure with an app update, you could use the technique I'm using. See my comment at the end of this thread for some code. Basically, you store the DB version as an app property, do a check at launch, and modify the DB structure as needed with ALTER TABLE. Be aware that Android will silently ignore errors in an ALTER command, say if you try to add a field and the field is already there. iOS will throw an error and halt.
-
I recommend the following, since you will be able to change the version (and get a brand new database). Then when you push an update you won't have database issues when the structure changes:
var databaseVersion = "1" function openDB() { return Titanium.Database.open("Database_" + databaseVersion); } var db = openDB(); db.close();
-
Thank you both for your answers, looks like either way should do the trick. I also appreciate the info on supporting database updates.
I guess this should have been more obvious to me, but you can also just delete the app through the Android "Manage Applications" interface, accomplishing roughly the same thing as deleting the iPhone generated folder.
-
probably this may help http://www.enterra-inc.com/techzone/handling_sql_issues/
-
you can also uninstall the app from emulator or the testing device then install again with your new database without having to change your code.