Skip to content

Instantly share code, notes, and snippets.

@ioseb
Created February 27, 2012 21:49
Show Gist options
  • Save ioseb/1927334 to your computer and use it in GitHub Desktop.
Save ioseb/1927334 to your computer and use it in GitHub Desktop.
package ge.gtug;
import android.content.Context;
import android.content.res.Resources;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class DatabaseHelper extends SQLiteOpenHelper {
//The Android's default system path of your application database.
private static String DB_PATH = "/data/data/ge.gtug/databases/";
private static String DB_NAME = "ilingoka";
private SQLiteDatabase myDataBase;
private final Context myContext;
private Resources resources;
public DatabaseHelper(Context context) {
super(context, DB_NAME, null, 1);
this.myContext = context;
resources = context.getResources();
}
@Override
public void onCreate(SQLiteDatabase arg0) {
// TODO Auto-generated method stub
}
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public synchronized void close() {
if(myDataBase != null) {
myDataBase.close();
}
super.close();
}
public void createDataBase() throws IOException{
boolean dbExist = checkDataBase();
if(dbExist){
//do nothing - database already exist
}else{
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 {
String outFileName = DB_PATH + DB_NAME;
OutputStream output = new FileOutputStream(outFileName);
copy(resources.openRawResource(R.raw.enggeoaa), output);
copy(resources.openRawResource(R.raw.enggeoab), output);
copy(resources.openRawResource(R.raw.enggeoac), output);
copy(resources.openRawResource(R.raw.enggeoad), output);
copy(resources.openRawResource(R.raw.enggeoae), output);
copy(resources.openRawResource(R.raw.enggeoaf), output);
copy(resources.openRawResource(R.raw.enggeoag), output);
copy(resources.openRawResource(R.raw.enggeoah), output);
copy(resources.openRawResource(R.raw.enggeoai), output);
output.flush();
output.close();
}
private void copy(InputStream input, OutputStream output) throws IOException {
byte[] buffer = new byte[1024];
int length;
while((length = input.read(buffer)) > 0) {
output.write(buffer);
output.flush();
}
input.close();
}
public void openDataBase() throws SQLException{
//Open the database
String myPath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment