Back to project page digitalcampus.
The source code is released under:
MIT License
If you think the Android project digitalcampus 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.llenguatges.digitalcampus.database; /* w ww . j a va2 s.co m*/ import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import android.content.Context; import android.database.DatabaseErrorHandler; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteException; import android.database.sqlite.SQLiteOpenHelper; public class DAOHelper extends SQLiteOpenHelper { private static String DATABASE_NAME = "digitalcampus.s3db"; private Context myContext; public String db_path; protected SQLiteDatabase db; /** * Constructor * @param context */ public DAOHelper(Context context) { super(context, DATABASE_NAME, null, 1); this.myContext = context; this.db_path = this.myContext.getDatabasePath(DATABASE_NAME).getPath(); } /** * Constructor * @param context * @param name * @param factory * @param version */ public DAOHelper(Context context, String name, CursorFactory factory, int version) { super(context, name, factory, version); } /** * Constructor * @param context * @param name * @param factory * @param version * @param errorHandler */ public DAOHelper(Context context, String name, CursorFactory factory, int version, DatabaseErrorHandler errorHandler) { super(context, name, factory, version, errorHandler); } /** * Called when the database is created for the first time. * This is where the creation of tables and the initial population of the tables should happen. */ public void onCreate(SQLiteDatabase db) { } /** * Called when the database needs to be upgraded. * The implementation should use this method to drop tables, * add tables, or do anything else it needs to upgrade to the new schema version. */ public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) { } /** * Copies your database from your local assets-folder to the just created empty database in the * system folder, from where it can be accessed and handled. * This is done by transferring byte stream. */ public void copyDataBase() throws IOException { synchronized (this) { db = getReadableDatabase(); // Open data base as the input stream InputStream myInput = this.myContext.getAssets().open(DATABASE_NAME); // Path to the just created empty data base String outFileName = this.db_path; // Open the empty data baseas the output stream OutputStream myOutput = new FileOutputStream(outFileName); // Transfer bytes from the input file to the output file byte[] buffer = new byte[1024]; int length; while ((length = myInput.read(buffer)) > 0) { myOutput.write(buffer, 0, length); } myOutput.flush(); myOutput.close(); myInput.close(); } } /** * Get readable database's instance. */ public void openReadableDB(){ this.db = getReadableDatabase(); } /** * Get writable database's instance. */ public void openWritableDB(){ this.db = getWritableDatabase(); } /** * Close any open database object. */ public void closeDB(){ if (this.db != null) this.db.close(); super.close(); } /** * Check if database exists. * @return checkDB */ public boolean checkDataBase(){ SQLiteDatabase checkDB = null; try{ checkDB = SQLiteDatabase.openDatabase(db_path, null, SQLiteDatabase.OPEN_READONLY); }catch(SQLiteException e){ }catch(Exception e){ } if(checkDB != null){ checkDB.close(); } return checkDB != null ? true : false; } }