Most all of the Android examples and tutorials out there assume you want to create and populate your database at run-time and not to use and access an independent, preloaded database with your Android application.
The method I’m going to show you takes your previous SQLite database file from the “assets” folder and copies into the system database path of your application so the SQLiteDatabase API can open and access it normally.
Copying, opening and accessing your database in your Android application.
Now just put your database file in the “assets” folder of your project and create a Database Helper class by extending the SQLiteOpenHelper class from the “android.database.sqlite” package.
Make your DataBaseHelper class look like this:
/**
*
*/
package com.dbz.database;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
/**
* @author admin
*
*/
public class DataBaseHelper extends SQLiteOpenHelper {
private static String DB_PATH = “/data/data/com.dbz.champs/databases/”;
private static String DB_NAME = “QuizApplication.sqlite”;
public static final String TABLE_NAME = “Exam_result”;
public static final String EXAM_TABLE_COLUMN_ID = “id”;
public static final String EXAM_TABLE_COLUMN_MARK = “mark”;
public static final String EXAM_TABLE_COLUMN_DATE = “date”;
public static final String EXAM_DATABASE_CREATE = “CREATE TABLE IF NOT EXISTS ” + TABLE_NAME
+ ” ( ” + EXAM_TABLE_COLUMN_ID + ” INTEGER PRIMARY KEY AUTOINCREMENT, ”
+ EXAM_TABLE_COLUMN_MARK + ” INTEGER NOT NULL, ”
+ EXAM_TABLE_COLUMN_DATE + ” DATETIME NOT NULL);”;
private SQLiteDatabase myDataBase;
private final Context myContext;
public DataBaseHelper(Context context) {
super(context, DB_NAME, null, 1);
this.myContext = context;
}
public void createDataBase() throws Exception {
boolean dbExist = checkDataBase();
if (dbExist) {
// do nothing – database already exist
} else {
// By calling this method and empty database will be created into
// the default system path
// of your application so we are gonna be able to overwrite that
// database with our database.
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
throw new Error(“Error copying database”);
}
}
}
private boolean checkDataBase() {
SQLiteDatabase checkDB = null;
try {
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READONLY);
} catch (SQLiteException e) {
// database does’t exist yet.
}
if (checkDB != null) {
checkDB.close();
}
return checkDB != null ? true : false;
}
private void copyDataBase() throws IOException {
// Open your local db as the input stream
InputStream myInput = myContext.getAssets().open(DB_NAME);
// Path to the just created empty db
String outFileName = DB_PATH + DB_NAME;
// Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
// transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
// Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
public void openDataBase() throws SQLException {
// Open the database
String myPath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READONLY);
}
public synchronized void close() {
if (myDataBase != null)
myDataBase.close();
super.close();
}
public void onCreate(SQLiteDatabase database) {
/// database.execSQL(EXAM_DATABASE_CREATE);
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}