My goal was to write a helper from scratch that retained pretty much all of the functionality of the original but added the ability to save the file onto the SD Card. Whether the database is external or not is defined simply with a boolean flag in the constructor. I made it a point not to hard-code the external location as I wanted Android to store the file dynamically based on the application. This also makes sure that the database is deleted upon application removal by default (I suppose I could add in the functionality to persist the database somehow but I believe that's something that should be done within the application itself).
Anyways, I'm pretty happy with what I came up with so far and I wanted to solicit feedback from people who actually know what their doing in the world of Java and Android. This is my first Java project outside of Hello World so I really want to know if I'm understanding the concepts and syntax. Fire away!
The OpenHelper class:
Using java Syntax Highlighting
- import java.io.File;
- import android.content.Context;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteDatabase.CursorFactory;
- import android.database.sqlite.SQLiteException;
- import android.util.Log;
- public abstract class KittDBHelper {
- private static final String TAG = "KittDBHelper";
- private final Context mContext;
- private final CursorFactory mCursorFactory;
- private final String DB_NAME;
- private final int DB_VERSION;
- private SQLiteDatabase mDB = null;
- private boolean DB_EXTERNAL = false;
- private String DB_PATH = "";
- public KittDBHelper(Context context, String name, CursorFactory factory, int version, boolean isExternal) {
- if(version < 1){
- throw new IllegalArgumentException("Version must be >= 1, version is: " + version + ".");
- }
- mContext = context;
- mCursorFactory = factory;
- DB_NAME = name;
- DB_VERSION = version;
- DB_EXTERNAL = isExternal;
- }
- //Return database with read only permission
- public synchronized SQLiteDatabase getReadableDatabase() {
- if(mDB != null && mDB.isOpen()){
- Log.d(TAG, "Database: " + DB_NAME + " is already open.");
- return mDB;
- }
- else if (DB_NAME == null){
- throw new SQLiteException("Cannot open a temporary database as READ ONLY.");
- }
- initializeDB();
- mDB.close();
- mDB = SQLiteDatabase.openDatabase(DB_PATH, mCursorFactory, SQLiteDatabase.OPEN_READONLY);
- Log.d(TAG, "Database: " + DB_NAME + " opened (READ ONLY).");
- return mDB;
- }
- //Return database with write permission
- public synchronized SQLiteDatabase getWritableDatabase() {
- if(mDB != null && mDB.isOpen() && !mDB.isReadOnly()){
- Log.d(TAG, "Database: " + DB_NAME + " is already open.");
- return mDB;
- }
- initializeDB();
- Log.d(TAG, "Database: " + DB_NAME + " opened (READ/WRITE).");
- return mDB;
- }
- //Perform necessary actions to create or update and/or open database
- private void initializeDB() {
- if(DB_NAME == null){
- mDB = SQLiteDatabase.create(null);
- }
- else{
- if(!DB_EXTERNAL){
- mDB = mContext.openOrCreateDatabase(DB_NAME, 0, mCursorFactory);
- DB_PATH = mContext.getDatabasePath(DB_NAME).getPath();
- }
- else{
- File f = new File(mContext.getExternalFilesDir(null), DB_NAME);
- mDB = SQLiteDatabase.openOrCreateDatabase(f, mCursorFactory);
- DB_PATH = f.getPath();
- }
- }
- int curVersion = mDB.getVersion();
- if(curVersion != DB_VERSION){
- mDB.beginTransaction();
- try{
- if(curVersion == 0){
- onCreate(mDB);
- }
- else{
- if(curVersion < DB_VERSION){
- onUpgrade(mDB, curVersion, DB_VERSION);
- }
- else{
- onDowngrade(mDB, curVersion, DB_VERSION);
- }
- }
- mDB.setVersion(DB_VERSION);
- mDB.setTransactionSuccessful();
- }
- finally{
- mDB.endTransaction();
- }
- }
- onOpen(mDB);
- Log.d(TAG, "Database: " + DB_NAME + " initialized.");
- }
- public void onOpen(SQLiteDatabase db)
- {
- // Optional: override function to perform additional functions on open.
- }
- public synchronized void close() {
- if(mDB != null && mDB.isOpen()){
- mDB.close();
- mDB = null;
- Log.d(TAG, "Database: " + DB_NAME + " closed.");
- }
- }
- public abstract void onCreate(SQLiteDatabase db);
- public abstract void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion);
- public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion)
- {
- throw new SQLiteException("Unable to downgrade database: " + DB_NAME + " from version " + mDB.getVersion() + " to version " + DB_VERSION + ". Override " + "onDowngrade method to provide this functionality.");
- }
- }
Parsed in 0.044 seconds, using GeSHi 1.0.8.4
The Database class:
Using java Syntax Highlighting
- import android.content.Context;
- import android.database.sqlite.SQLiteDatabase;
- import android.util.Log;
- public class Database extends KittDBHelper {
- static final String TAG = "Kitt_DB";
- //General database settings
- static final String DB_NAME = "Kitt.db";
- static final int DB_VERSION = 6;
- static final boolean DB_EXTERNAL = false;
- //Database table map
- public static final int TABLE_ONE = 1;
- public static final int TABLE_TWO = 2;
- public static final int TABLE_THREE = 3;
- public static final int arrDB_TABLES[] = { TABLE_ONE, TABLE_TWO, TABLE_THREE };
- Context context = null;
- public Database(Context context) {
- super(context, DB_NAME, null, DB_VERSION, DB_EXTERNAL);
- this.context = context;
- }
- @Override
- public void onCreate(SQLiteDatabase db) {
- final String SQL_USERS = "create table tbl" + TABLE_ONE + " (u_id int primary key, u_last_modified int, u_name text)";
- final String SQL_CHAMPIONS = "create table tbl" + TABLE_TWO + " (c_id int primary key, c_last_modified int, c_name text)";
- final String SQL_ABILITIES = "create table tbl" + TABLE_THREE + " (a_id int primary key, a_last_modified int, a_name text)";
- final String arrDB_SQL[] = { SQL_USERS, SQL_CHAMPIONS, SQL_ABILITIES };
- for (int i : arrDB_TABLES)
- {
- db.execSQL(arrDB_SQL[i-1]);
- Log.d(TAG, "Table: " + arrDB_TABLES[i-1] + " | SQL: " + arrDB_SQL[i-1] + ";");
- }
- }
- @Override
- public void onUpgrade(SQLiteDatabase db, int prevVersion, int newVersion) {
- //Delete all tables
- for (int i : arrDB_TABLES)
- {
- db.execSQL("drop table if exists tbl" + arrDB_TABLES[i-1] + ";");
- Log.d(TAG, "Table: " + arrDB_TABLES[i-1] + " was deleted.");
- }
- //Recreate database
- onCreate(db);
- Log.d(TAG, "Database " + DB_NAME + " was upgraded.");
- }
- }
Parsed in 0.039 seconds, using GeSHi 1.0.8.4
Here is the actual source exported from Eclipse (it's a lot prettier and easier to read IMO).

