Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

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?

— asked December 18th 2010 by Mike Dosey
  • android
  • database
  • mobile
  • reset
  • sqlite
0 Comments

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
    
    — answered December 21st 2010 by Don Thorp
    permalink
    0 Comments
  • 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.

    — answered December 18th 2010 by Doug Handy
    permalink
    1 Comment
    • Do i need to give the virtual DB name or the name of the DB created by me ?

      — commented March 25th 2011 by Primoris online
  • 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.

    — answered December 21st 2010 by Tim Poulsen
    permalink
    1 Comment
    • While the other ideas here may work, this is probably the 'right' way to do it. Check out the adb docs for a how-to on using adb to open a shell on the emulator, from which you can execute SQL commands against a database.

      — commented December 21st 2010 by Kevin Whinnery
  • 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();
    
    — answered December 18th 2010 by John Welch
    permalink
    0 Comments
  • 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.

    — answered December 20th 2010 by Mike Dosey
    permalink
    0 Comments
  • probably this may help http://www.enterra-inc.com/techzone/handling_sql_issues/

    — answered July 4th 2012 by Den Brown
    permalink
    0 Comments
  • 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.

    — answered August 11th 2012 by Talal Manaa
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.