Android Open Source - SimpleStorage Database






From Project

Back to project page SimpleStorage.

License

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.

Java Source Code

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();
        }
      }
    }
  }
}




Java Source Code List

com.njzk2.simplestorage.DataProvider.java
com.njzk2.simplestorage.Database.java
com.njzk2.simplestorage.SQLHelper.java
com.njzk2.simplestorage.SQLSchema.java
com.njzk2.simplestorage.Storable.java
com.njzk2.simplestorage.handler.BooleanHandler.java
com.njzk2.simplestorage.handler.BytesHandler.java
com.njzk2.simplestorage.handler.DateHandler.java
com.njzk2.simplestorage.handler.IntHandler.java
com.njzk2.simplestorage.handler.LongHandler.java
com.njzk2.simplestorage.handler.StringArrayHandler.java
com.njzk2.simplestorage.handler.StringHandler.java
com.njzk2.simplestorage.handler.TypeHandler.java