Back to project page SimpleStorage.
The source code is released under:
Apache License
If you think the Android project SimpleStorage listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.njzk2.simplestorage; /* ww w . ja v a 2 s .c o m*/ import java.util.ArrayList; import java.util.Arrays; import java.util.List; import android.content.Context; import android.content.pm.PackageManager.NameNotFoundException; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class Database extends SQLiteOpenHelper { public static final List<Class<? extends Storable>> TABLES = new ArrayList<Class<? extends Storable>>(); private static Database instance = null; public static Database getInstance(Context context) { if (instance == null) { try { instance = new Database(context.getApplicationContext()); } catch (NameNotFoundException e) { e.printStackTrace(); } } return instance; } private Database(Context context) throws NameNotFoundException { super(context, "simplestorage", null, context.getPackageManager() .getPackageInfo(context.getPackageName(), 0).versionCode); } @Override public void onCreate(SQLiteDatabase db) { for (Class<? extends Storable> table : TABLES) { // TODO assert that the class has the mandatory empty constructor db.execSQL(new SQLSchema(table).toString()); } } @Override public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) { // Make sure all table exist onCreate(db); // Upgrade if necessary for (Class<? extends Storable> table : TABLES) { String tableName = SQLHelper.getTableName(table); Cursor c = db.query(tableName, null, null, null, null, null, null, "0"); List<String> columnNames = Arrays.asList(c.getColumnNames()); for (String sql : new SQLSchema(table).toAlterStrings(columnNames)) { try { db.execSQL(sql); } catch (Exception e) { e.printStackTrace(); } } } } }