Created
July 6, 2013 20:17
-
-
Save kiichi/5941130 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
USAGE: | |
DatabaseHelper databaseHelper = new DatabaseHelper(getActivity()); | |
SQLiteDatabase sqLiteDatabase; | |
try { | |
databaseHelper.createEmptyDataBase(); | |
sqLiteDatabase = databaseHelper.openDataBase(); | |
Cursor cursor = sqLiteDatabase.rawQuery("SELECT * FROM locations",null); | |
cursor.moveToFirst(); | |
while(cursor.moveToNext()){ | |
Log.v("myapp",cursor.getString(cursor.getColumnIndex("address"))); | |
} | |
cursor.close(); | |
} | |
catch (IOException e) { | |
Log.v("myapp","IOException Database Creation ERROR: " + e.getMessage()); | |
} | |
*/ | |
package com.yourdomain.yourpackagename; | |
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.SQLiteDatabase.CursorFactory; | |
import android.database.sqlite.SQLiteException; | |
import android.database.sqlite.SQLiteOpenHelper; | |
public class DatabaseHelper extends SQLiteOpenHelper { | |
private static String DB_PATH = "/data/data/com.yourdomain.yourpackagename/databases/"; | |
private static String DB_NAME = "pom"; | |
private static String DB_NAME_ASSET = "pom.db"; | |
private SQLiteDatabase mDataBase; | |
private final Context mContext; | |
public DatabaseHelper(Context context) { | |
super(context, DB_NAME, null, 1); | |
this.mContext = context; | |
} | |
public void createEmptyDataBase() throws IOException{ | |
boolean dbExist = checkDataBaseExists(); | |
if(dbExist){ | |
// do nothing | |
} | |
else{ | |
this.getReadableDatabase(); | |
try { | |
copyDataBaseFromAsset(); | |
} catch (IOException e) { | |
throw new Error("Error copying database"); | |
} | |
} | |
} | |
private boolean checkDataBaseExists() { | |
SQLiteDatabase checkDb = null; | |
try{ | |
String dbPath = DB_PATH + DB_NAME; | |
checkDb = SQLiteDatabase.openDatabase(dbPath, null, SQLiteDatabase.OPEN_READONLY); | |
} | |
catch(SQLiteException e){ | |
// | |
} | |
if(checkDb != null){ | |
checkDb.close(); | |
} | |
return checkDb != null ? true : false; | |
} | |
private void copyDataBaseFromAsset() throws IOException{ | |
InputStream mInput = mContext.getAssets().open(DB_NAME_ASSET); | |
String outFileName = DB_PATH + DB_NAME; | |
OutputStream mOutput = new FileOutputStream(outFileName); | |
byte[] buffer = new byte[1024]; | |
int size; | |
while ((size = mInput.read(buffer)) > 0){ | |
mOutput.write(buffer, 0, size); | |
} | |
mOutput.flush(); | |
mOutput.close(); | |
mInput.close(); | |
} | |
public SQLiteDatabase openDataBase() throws SQLException { | |
String myPath = DB_PATH + DB_NAME; | |
mDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY); | |
return mDataBase; | |
} | |
@Override | |
public void onCreate(SQLiteDatabase arg0) { | |
} | |
@Override | |
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { | |
} | |
@Override | |
public synchronized void close() { | |
if(mDataBase != null) { | |
mDataBase.close(); | |
} | |
super.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment