Skip to content

Commit

Permalink
Handled exceptions that may occur on onUpgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
Cristian committed Sep 5, 2012
1 parent c32c654 commit 293c181
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ bin
target
tmp
.clover
atlassian-ide-plugin.xml
21 changes: 15 additions & 6 deletions src/main/java/com/codeslap/persistence/SqliteDb.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

import java.util.HashMap;
Expand Down Expand Up @@ -63,16 +64,24 @@ public Helper(Context context, String name, int version) {
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Here we will the whole database and recreate it
Cursor c = db.rawQuery("SELECT 'DROP TABLE ' || name || ';' FROM sqlite_master WHERE type = 'table' " +
Cursor cursor = db.rawQuery("SELECT 'DROP TABLE ' || name || ';' FROM sqlite_master WHERE type = 'table' " +
"AND name != 'sqlite_sequence';", null);
if (c != null && c.moveToFirst()) {
if (cursor != null && cursor.moveToFirst()) {
do {
db.execSQL(c.getString(0));
} while (c.moveToNext());
try {
db.execSQL(cursor.getString(0));
} catch (SQLException e) {
// lets ignore errors while purging the database
}
} while (cursor.moveToNext());
}
try {
db.execSQL("DELETE FROM sqlite_sequence");
} catch (SQLException e) {
// sometimes the sqlite_sequence has not been created yet
}
db.execSQL("DELETE FROM sqlite_sequence");
onCreate(db);
c.close();
cursor.close();
}
}
}

0 comments on commit 293c181

Please sign in to comment.