Using Robolectric With Sqliteassethelper
I'm very new to Robolectric, so apologies in advance if I'm missing something obvious here. I have an app that loads a database from the assets directory using an SQLiteAssetHelper
Solution 1:
First copy your database from asset folder to your app database folder. Here is the code that will copy the database
publicclassDataBaseWrapperextendsSQLiteOpenHelper
{
privatestaticStringTAG= DataBaseWrapper.class.getName();
private String DB_PATH; //= "/data/data/com.example.yourproject/databases/";privatestaticStringDB_NAME="Database.sqlite";
privateSQLiteDatabasemyDataBase=null;
privatefinal Context myContext;
publicDataBaseWrapper(Context context)
{
super(context, DB_NAME, null, 1);
this.myContext = context;
DB_PATH="/data/data/" + context.getPackageName() + "/" + "databases/";
Log.v("log_tag", "DBPath: " + DB_PATH);
// File f=getDatabasePath(DB_NAME);
}
publicvoidcreateDataBase()throws IOException{
booleandbExist= checkDataBase();
if(dbExist){
Log.v("log_tag", "database does exist");
}else{
Log.v("log_tag", "database does not exist");
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
thrownewError("Error copying database");
}
}
}
privatevoidcopyDataBase()throws IOException{
InputStreammyInput= myContext.getAssets().open(DB_NAME);
StringoutFileName= DB_PATH + DB_NAME;
OutputStreammyOutput=newFileOutputStream(outFileName);
byte[] buffer = newbyte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
myInput.close();
}
privatebooleancheckDataBase(){
FiledbFile=newFile(DB_PATH + DB_NAME);
//Log.v("dbFile", dbFile + " "+ dbFile.exists()); return dbFile.exists();
}
publicbooleanopenDataBase()throws SQLException
{
StringmPath= DB_PATH + DB_NAME;
//Log.v("mPath", mPath);
myDataBase = SQLiteDatabase.openDatabase(mPath, null, SQLiteDatabase.CREATE_IF_NECESSARY);
//mDataBase = SQLiteDatabase.openDatabase(mPath, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS); return myDataBase != null;
}
@Overridepublicsynchronizedvoidclose()
{
if(myDataBase != null)
myDataBase.close();
super.close();
}
@OverridepublicvoidonCreate(SQLiteDatabase db)
{
}
@OverridepublicvoidonUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
Log.v(TAG, "Upgrading database, this will drop database and recreate.");
}
}
Also chk this link .. it might be useful for u Testing SQLite database in Robolectric
Try these things:
1.Delete your database from terminal
adb shell
cd /data/data/com.example.apploicationname/databases
rm *
and re-install your application again on emulator.
Try to increase the RAM of emulator.. Same error was coming to me despite of my all the data in the database but when I increased the RAM of emulator from 1024 to 2000 .. It worked.
Copy your database from DDMS to your system file system and open it thru sqlite browser and check whether Table exist or not. Link for sqlite browser http://sourceforge.net/projects/sqlitebrowser/files/sqlitebrowser/
Post a Comment for "Using Robolectric With Sqliteassethelper"