Created
July 30, 2017 06:53
-
-
Save tonY1883/e3c8d4c788a6b42cd8647b33eee43a5b to your computer and use it in GitHub Desktop.
Android SQLiteHelper with auto upgrade and downgrade
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private class SQLiteHelper extends SQLiteOpenHelper { | |
public DatabaseOperations(Context context) { | |
super(context, DatabaseSchemas.DATABASE_NAME, null, DatabaseSchemas.DATABASE_VERSION); | |
} | |
public void onCreate(SQLiteDatabase db) { | |
//Create all tables | |
db.execSQL(DatabaseSchemas.Table.CREATION_SQL); | |
} | |
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { | |
//Make all table names in a List. This saves a lot of effort. | |
List<String> tableList = new ArrayList<>(); | |
Cursor c = db.query( | |
"sqlite_master", | |
new String[]{ | |
"name" | |
}, | |
"type = ? AND name != ?", | |
new String[]{ | |
"table", | |
"android_metadata" | |
}, null, null, "name", null); | |
if (c != null) { | |
if (c.moveToFirst()) { | |
while (! c.isAfterLast()) { | |
tableList.add(c.getString(c.getColumnIndex("name"))); | |
c.moveToNext(); | |
} | |
} | |
c.close(); | |
} | |
//backup the content of old database. | |
for (int i = 0; i < tableList.size(); i++) { | |
db.execSQL("ALTER TABLE " + tableList.get(i) + " RENAME TO " + "old" + tableList.get(i)); | |
} | |
//create new tables | |
db.execSQL(DatabaseSchemas.Table.CREATION_SQL); | |
//exclude any tables that no longer present | |
//WARNING: possible data loss | |
List<String> ntableList = new ArrayList<>(); | |
c = db.query( | |
"sqlite_master", | |
new String[]{ | |
"name" | |
}, | |
"type = ? AND name != ? AND name NOT LIKE ?", | |
new String[]{ | |
"table", | |
"android_metadata", | |
"old%" | |
}, null, null, "name", null); | |
if (c != null) { | |
if (c.moveToFirst()) { | |
while (! c.isAfterLast()) { | |
ntableList.add(c.getString(c.getColumnIndex("name"))); | |
c.moveToNext(); | |
} | |
} | |
c.close(); | |
} | |
tableList.retainAll(ntableList); | |
//move data into new tables | |
for (int i = 0; i < tableList.size(); i++) { | |
List<String> columns = GetColumns(db, "old" + tableList.get(i)); | |
columns.retainAll(GetColumns(db, tableList.get(i))); | |
String cols = TextUtils.join(",", columns); | |
db.execSQL(String.format("INSERT INTO %s (%s) SELECT %s FROM old%s", tableList.get(i), cols, cols, tableList.get(i))); | |
db.execSQL("DROP TABLE " + "old" + tableList.get(i)); | |
} | |
} | |
@Override | |
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) { | |
//basically the same stuff, so we just call onUpgrade here | |
onUpgrade(db, oldVersion, newVersion); | |
} | |
private List<String> GetColumns(SQLiteDatabase db, String tableName) { | |
List<String> columns = null; | |
Cursor c = null; | |
try { | |
c = db.query(tableName, null, null, null, null, null, null); | |
if (c != null) { | |
columns = new ArrayList<String>(Arrays.asList(c.getColumnNames())); | |
} | |
} catch (Exception e) { | |
Log.e(tableName, e.getMessage(), e); | |
e.printStackTrace(); | |
} finally { | |
if (c != null) | |
c.close(); | |
} | |
return columns; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment