It uses the SQL interface from the notepad tutorial, stripped down to what it needs.
The SQL controller (master)
Using java Syntax Highlighting
- public class SqlMaster {
- public static final String KEY_VERSION = "version";
- public static final String KEY_QUERY = "query";
- public static final String DATABASE_NAME = "data";
- public static final String TABLE_SQL_UPDATES = "sql_updates";
- private static final String TAG = "SqlMaster";
- private static final int DATABASE_VERSION = 2;
- private DatabaseHelper mDbHelper;
- private SQLiteDatabase mDb;
- private final Context mCtx;
- private static class DatabaseHelper extends SQLiteOpenHelper {
- DatabaseHelper(Context context) {
- super(context, DATABASE_NAME, null, DATABASE_VERSION);
- }
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
- + newVersion + ", which will destroy all old data");
- db.execSQL("DROP TABLE IF EXISTS sql_updates");
- onCreate(db);
- }
- @Override
- public void onCreate(SQLiteDatabase db) {
- }
- }
- public SqlMaster(Context ctx) {
- this.mCtx = ctx;
- }
- public SqlMaster open() throws SQLException {
- mDbHelper = new DatabaseHelper(mCtx);
- mDb = mDbHelper.getWritableDatabase();
- return this;
- }
- public void close() {
- mDbHelper.close();
- }
- public int getOldVersion() {
- String tableQuery = "select " + KEY_VERSION + " from " + TABLE_SQL_UPDATES;
- Cursor tableCursor = mDb.rawQuery(tableQuery, null);
- if(tableCursor.getCount() == 0) {
- return 0;
- } else {
- tableCursor.moveToLast();
- return tableCursor.getInt(tableCursor.getColumnIndexOrThrow(KEY_VERSION));
- }
- }
- public long addNewVersion(Version u) {
- ContentValues initialValues = u.getValues();
- return mDb.insert(TABLE_SQL_UPDATES, null, initialValues);
- }
- public boolean updateToVersion(int newVersion, int oldVersion) {
- String query = "select * from " + TABLE_SQL_UPDATES + " where " + KEY_VERSION + " > " + oldVersion;
- Cursor updates = mDb.rawQuery(query, null);
- if(updates.getCount() <= 0) {
- return false;
- }
- updates.moveToFirst();
- int workVer = updates.getInt(updates.getColumnIndexOrThrow(KEY_VERSION));
- while(workVer <= newVersion) {
- String q = updates.getString(updates.getColumnIndexOrThrow(KEY_QUERY));
- mDb.execSQL(q);
- if(workVer < newVersion) {
- updates.moveToNext();
- }
- if(workVer == newVersion){
- break;
- }
- }
- return true;
- }
- public void clearTable(String table) {
- mDb.execSQL("DELETE FROM " + table);
- }
- }
Parsed in 0.014 seconds, using GeSHi 1.0.8.4
The Update Controller
Using java Syntax Highlighting
- class UpdateSystem {
- static int numberOfUpdates = 5;
- static Version updates[] = {
- new Version(110,"CREATE TABLE tester (row1 int, row2 int)"),
- new Version(111,"ALTER TABLE tester ADD row3 char(4) default 'abcd'"),
- new Version(111,"ALTER TABLE tester ADD newcolumn int"),
- new Version(200,"CREATE TABLE diditwork (row1 int, row2 char(3))"),
- new Version(202,"ALTER TABLE diditwork ADD newrow int")
- };
- static void updateSql(Context ctx) {
- SqlMaster mDbHelper = new SqlMaster(ctx);
- mDbHelper.open();
- //get last known version + newest version
- int currVer = 0;
- int oldVer = 0;
- try {
- currVer = ctx.getPackageManager().getPackageInfo(ctx.getPackageName(), 0).versionCode;
- } catch (NameNotFoundException e) {
- e.printStackTrace();
- }
- oldVer = mDbHelper.getOldVersion();
- //Replace sql queries
- mDbHelper.clearTable(SqlMaster.TABLE_SQL_UPDATES);
- for(int i = 0;numberOfUpdates>i;i++) {
- mDbHelper.addNewVersion(updates[i]);
- }
- //start running SQL commands from last recorded version # + 1
- //if we dont have an old update line, run newest one
- if(oldVer == 0) {
- oldVer = currVer - 1;
- }
- if(currVer > oldVer) {
- mDbHelper.updateToVersion(currVer, oldVer);
- }
- mDbHelper.close();
- }
- }
Parsed in 0.012 seconds, using GeSHi 1.0.8.4
A version class to hold update info
Using java Syntax Highlighting
- public class Version {
- public int mVersion;
- public String mQuery;
- Version(int version, String query) {
- mVersion = version;
- mQuery = query;
- }
- public ContentValues getValues() {
- ContentValues c = new ContentValues();
- c.put(SqlMaster.KEY_VERSION, mVersion);
- c.put(SqlMaster.KEY_QUERY, mQuery);
- return c;
- }
- }
Parsed in 0.010 seconds, using GeSHi 1.0.8.4
Add items to the updates array in UpdateSystem with the version # and SQL command, change the numberofupdates to the number of updates, and use the command UpdateSystem.updateSql(this); when your application starts to use it
