1

I implemented an Android application which use Sqlite database.

When I release a new version of my application (not in playStore), I upload it on my server, so, if the old application is running, calling web service, can understand that new version is available. So, new version is downloaded and installed.

When the application is overinstalled, the database is not dropped, so if I need to do any changes of my database I need to use the method:

public void onUpgrade(SQLiteDatabase _db, int oldVersion, int newVersion)

This is fine, unless I need to do many changes in my database. In this case, the code becomes unreadable. So I would to delete the database and create a new one.

How can I perform this task?

EDIT: What about using context.deleteDatabase(DATABASE_NAME); ?

1
  • Have you tried to delete the database file? You can get the path with _db.getPath(). Commented Apr 29, 2013 at 14:53

3 Answers 3

7

For throw-away databases (where the data is e.g. a cached copy of data available in the cloud) I usually make onUpgrade() just call onCreate() and make onCreate() execute DROP TABLE IF EXISTS <tablename> before creating the tables.

For example:

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  onCreate(db);
}

@Override
public void onCreate(SQLiteDatabase db) {
  db.execSQL("DROP TABLE IF EXISTS foo");
  db.execSQL("CREATE TABLE foo(bar INTEGER, baz TEXT");
}
4
  • But my table are yet created, and I want drop it before recreate the new one.
    – GVillani82
    Commented Apr 29, 2013 at 15:14
  • That's exactly what DROP TABLE IF EXISTS does. It drops the table if it exists and does nothing if it doesn't. Edited to add an example.
    – laalto
    Commented Apr 29, 2013 at 15:18
  • right parenthesis is missing in 2nd execSQL method on onCreate()
    – ssi-anik
    Commented May 8, 2015 at 19:14
  • This is not a complete solution because it reuires that you know all the tables that already exist.
    – f470071
    Commented Sep 26, 2015 at 8:23
1

you can think about this one

keep a trace of your upgrade using a flag in shared preference. when you are downloading new version then set the flag to true. on every launch check the flag. if the flag is true then recreate the database and set the flag false.

I havn't tried similar things but I think it should work in your case.

and to delete database context.deleteDatabase(DATABASE_NAME);

1
  • 1
    Thank you @Stine, but when I talk about "deleting database" I mean drop database. How can I drop database?
    – GVillani82
    Commented Apr 29, 2013 at 14:52
0

If you just want to re-create your whole database, just drop every table in the old one.

1
  • 1
    And what will be the new database version?
    – GVillani82
    Commented Apr 29, 2013 at 15:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.