Last active
February 13, 2016 22:44
-
-
Save JoachimR/e0d2dca43497b4d34784 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
package my; | |
import android.content.Context; | |
import android.database.sqlite.SQLiteDatabase; | |
import android.database.sqlite.SQLiteDatabase.CursorFactory; | |
import android.database.sqlite.SQLiteException; | |
import android.util.Log; | |
import my.DaoMaster; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.OutputStream; | |
import static android.database.sqlite.SQLiteDatabase.OPEN_READONLY; | |
public class DatabaseOpenHelper extends DaoMaster.OpenHelper { | |
private static final String LOG_TAG = DatabaseOpenHelper.class.getSimpleName(); | |
private static final String ASSETS_DATABASE_FILE_NAME = "downloadedDb"; | |
private static final String INTERNAL_DATABASE_NAME = "downloadedDb"; | |
private String internalDatabaseAbsolutePath; | |
private Context context; | |
public DatabaseOpenHelper(Context context, String name, CursorFactory factory) { | |
super(context, name, factory); | |
this.context = context; | |
internalDatabaseAbsolutePath = context.getDatabasePath(INTERNAL_DATABASE_NAME).getAbsolutePath(); | |
try { | |
createDataBase(); | |
} catch (Exception ioe) { | |
throw new Error("Unable to create database"); | |
} | |
} | |
public void createDataBase() { | |
if (!databaseExists()) { | |
SQLiteDatabase sqliteDatabase = this.getReadableDatabase(); | |
sqliteDatabase.close(); | |
copyDataBase(); | |
} | |
} | |
private boolean databaseExists() { | |
SQLiteDatabase db = null; | |
try { | |
db = SQLiteDatabase.openDatabase(internalDatabaseAbsolutePath, null, OPEN_READONLY); | |
} catch (SQLiteException e) { | |
Log.w(LOG_TAG, "Error when trying to open database", e); | |
} finally { | |
if (db != null) { | |
db.close(); | |
} | |
} | |
return db != null; | |
} | |
public void copyDataBase() { | |
int length; | |
byte[] buffer = new byte[1024]; | |
try { | |
InputStream assetsFile = this.context.getAssets().open(ASSETS_DATABASE_FILE_NAME); | |
OutputStream outputStream = new FileOutputStream(internalDatabaseAbsolutePath); | |
while ((length = assetsFile.read(buffer)) > 0) { | |
outputStream.write(buffer, 0, length); | |
outputStream.flush(); | |
} | |
assetsFile.close(); | |
outputStream.close(); | |
} catch (IOException e) { | |
Log.w(LOG_TAG, "Error when trying to copy database", e); | |
} | |
} | |
@Override | |
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { | |
Log.i(LOG_TAG, String.format("Upgrading schema from version %d to %d by dropping all tables", | |
oldVersion, newVersion)); | |
DaoMaster.dropAllTables(db, true); | |
createDataBase(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment