Created
November 27, 2010 11:05
-
-
Save petershine/717801 to your computer and use it in GitHub Desktop.
Simple separator / assembler for installing big SQLite file onto Android Virtual Device (not yet tested for actual device)
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
/*** DBSeparator.java ***/ | |
import java.io.BufferedInputStream; | |
import java.io.BufferedOutputStream; | |
import java.io.FileInputStream; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
public class DBSeparator { | |
public static void main(String[] args) { | |
try { | |
SeparatorDB(); | |
} | |
catch (IOException e) { | |
System.out.println("SeparatorDB failed"); | |
} | |
} | |
private static void SeparatorDB() throws IOException { | |
FileInputStream fis = new FileInputStream("dbFiles/bigDatabase.sqlite"); | |
BufferedInputStream bis = new BufferedInputStream(fis); | |
FileOutputStream fos = new FileOutputStream("dbFiles/smallFile1.db");; | |
BufferedOutputStream bos = new BufferedOutputStream(fos);; | |
byte[] b = new byte[1024]; | |
int read = -1; | |
int count = 1; | |
int count2 = 1; | |
while((read = bis.read(b, 0, 1024)) != -1) { | |
bos.write(b, 0, read); | |
bos.flush(); | |
if(count2 % 900 == 0) { | |
count++; | |
if(fos != null) fos.close(); | |
if(bos != null) bos.close(); | |
fos = new FileOutputStream("dbFiles/smallFile" + count + ".db"); | |
bos = new BufferedOutputStream(fos); | |
} | |
count2++; | |
} | |
System.out.println("read: "+read+" count: "+count+" count2: "+count2); | |
fis.close(); | |
bis.close(); | |
if(fos != null) fos.close(); | |
if(bos != null) bos.close(); | |
} | |
} | |
/*** DataBaseHelper.java ***/ | |
import java.io.BufferedInputStream; | |
import java.io.BufferedOutputStream; | |
import java.io.File; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import android.content.Context; | |
import android.content.res.AssetManager; | |
import android.database.SQLException; | |
import android.database.sqlite.SQLiteDatabase; | |
import android.database.sqlite.SQLiteException; | |
import android.database.sqlite.SQLiteOpenHelper; | |
import android.util.Log; | |
public class DataBaseHelper extends SQLiteOpenHelper { | |
private static int numofSeparatedFiles = 9; // This can be different depends on the size of DB | |
private static String separatedFileName = "smallFile"; | |
private static String separatedFileExtension = ".db"; | |
private static String DB_PATH; | |
private static String DB_NAME; | |
private static SQLiteDatabase myDataBase; | |
private static Context myContext; | |
public DataBaseHelper(Context context, String db_path, String db_name) { | |
super(context, db_name, null, 1); | |
myContext = context; | |
DB_PATH = db_path; | |
DB_NAME = db_name; | |
} | |
public void createDataBase() throws IOException{ | |
boolean dbExist = checkDataBase(); | |
if (dbExist) { | |
Log.v(project.TAG, "dbExist = TRUE"); | |
} | |
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. | |
getReadableDatabase(); | |
try { | |
//copyDataBase(); | |
assembleDataBase(); | |
} catch (IOException e) { | |
throw new Error("Error copying(or assembling) database"); | |
} | |
} | |
Log.v(project.TAG, "createDataBase(): DONE"); | |
} | |
/** | |
* Check if the database already exist to avoid re-copying the file each time you open the application. | |
* @return true if it exists, false if it doesn't | |
*/ | |
private static boolean checkDataBase(){ | |
SQLiteDatabase checkDB = null; | |
try{ | |
String myPath = DB_PATH + DB_NAME; | |
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY); | |
}catch(SQLiteException e){ | |
Log.e(project.TAG, "database does't exist yet"); | |
} | |
if(checkDB != null){ | |
checkDB.close(); | |
} | |
Log.v(project.TAG, "checkDataBase(): DONE"); | |
return checkDB != null ? true : false; | |
} | |
/** | |
* 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 transfering bytestream. | |
* */ | |
/* | |
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); | |
Log.v(project.TAG, "myInput.read(buffer): "+length); | |
} | |
//Close the streams | |
myOutput.flush(); | |
myOutput.close(); | |
myInput.close(); | |
Log.v(project.TAG, "copyDataBase(): DONE"); | |
} | |
*/ | |
private static void assembleDataBase() throws IOException { | |
AssetManager myAssetManager = null; | |
InputStream[] separatedInput = new InputStream[numofSeparatedFiles]; // Number of separated files which will be assembled | |
BufferedInputStream[] separatedBuffer = new BufferedInputStream[numofSeparatedFiles]; | |
FileOutputStream assembledOutput = null; | |
BufferedOutputStream assembledBuffer = null; | |
try { | |
File newlyAssembled = new File(DB_PATH + DB_NAME); | |
Log.v(project.TAG, "newlyAssembled: "+newlyAssembled); | |
/* This is not necessary, since it's already checked | |
if(newlyAssembled.exists()) { | |
newlyAssembled.delete(); | |
newlyAssembled.createNewFile(); | |
} | |
*/ | |
myAssetManager = myContext.getResources().getAssets(); | |
for(int i = 0; i < separatedInput.length; i++) { | |
separatedInput[i] = myAssetManager.open(separatedFileName + (i + 1) + separatedFileExtension); | |
separatedBuffer[i] = new BufferedInputStream(separatedInput[i]); | |
} | |
assembledOutput = new FileOutputStream(newlyAssembled); | |
assembledBuffer = new BufferedOutputStream(assembledOutput); | |
int read = -1; | |
byte[] buffer = new byte[1024]; | |
for(int i = 0; i < separatedInput.length; i++) { | |
while((read = separatedBuffer[i].read(buffer, 0, 1024)) != -1) { | |
assembledBuffer.write(buffer, 0, read); | |
} | |
assembledBuffer.flush(); | |
Log.v(project.TAG, "Wrote InputStream["+i+"] successfully"); | |
} | |
Log.v(project.TAG, "Assembling Succeeded!"); | |
} | |
catch(Exception e) { | |
Log.v(project.TAG, "Assembling FAILED..."); | |
} | |
finally { | |
for(int i = 0; i < separatedInput.length; i++) { | |
try{if(separatedInput[i] != null) separatedInput[i].close();}catch(Exception e){} | |
try{if(separatedBuffer[i] != null) separatedBuffer[i].close();}catch(Exception e){} | |
} | |
try{if(assembledOutput != null) assembledOutput.close();}catch(Exception e){} | |
try{if(assembledBuffer != null) assembledBuffer.close();}catch(Exception e){} | |
separatedInput = null; | |
separatedBuffer = null; | |
} | |
} | |
public void openDataBase() throws SQLException{ | |
//Open the database | |
String myPath = DB_PATH + DB_NAME; | |
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY); | |
Log.v(project.TAG, "openDataBase(): DONE"); | |
} | |
@Override | |
public synchronized SQLiteDatabase getReadableDatabase() { | |
return myDataBase; | |
} | |
@Override | |
public synchronized void close() { | |
if(myDataBase != null) | |
myDataBase.close(); | |
super.close(); | |
Log.v(project.TAG, "synchronized void close()"); | |
} | |
@Override | |
public void onCreate(SQLiteDatabase db) { | |
Log.v(project.TAG, "onCreate(SQLiteDatabase db)"); | |
} | |
@Override | |
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { | |
Log.v(project.TAG, "onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment