Reputation: 1701
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, and I'd like to test that database using the Robolectric framework. I'm able to get a reference to the database, but it seems to be empty. SQLiteAssetHelper outputs a "successfully opened database" message to the log, but there are no tables in the database.
Edit: Here's the code I'm using to get the database in my setUp
method using the class @user2953017 posted below:
@Before
public void setUp() {
Context context = Robolectric.getShadowApplication().getApplicationContext();
SQLiteDatabase db = (new DataBaseWrapper(context)).getReadableDatabase();
Cursor c = db.rawQuery("SELECT name FROM sqlite_master WHERE type='table'", null);
if (!c.moveToFirst()) {
Log.w("debug", "No tables!");
}
// My second query here fails, since the database contains no tables.
}
From the logcat:
W/debug: No tables!
DEBUG: Loading resources for android from jar:/home/<name>/.m2/repository/org/robolectric/android-res/4.1.2_r1_rc/android-res-4.1.2_r1_rc-real.jar!/res...
E/Debug: java.lang.RuntimeException: SQL exception in query
The cause of the SQL exception is that the table name specified in the query isn't found.
I'd be grateful for any advice.
Upvotes: 3
Views: 740
Reputation: 5302
First copy your database from asset folder to your app database folder. Here is the code that will copy the database
public class DataBaseWrapper extends SQLiteOpenHelper
{
private static String TAG = DataBaseWrapper.class.getName();
private String DB_PATH; //= "/data/data/com.example.yourproject/databases/";
private static String DB_NAME = "Database.sqlite";
private SQLiteDatabase myDataBase = null;
private final Context myContext;
public DataBaseWrapper(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);
}
public void createDataBase() throws IOException{
boolean dbExist = 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) {
throw new Error("Error copying database");
}
}
}
private void copyDataBase() throws IOException{
InputStream myInput = myContext.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
myInput.close();
}
private boolean checkDataBase(){
File dbFile = new File(DB_PATH + DB_NAME);
//Log.v("dbFile", dbFile + " "+ dbFile.exists());
return dbFile.exists();
}
public boolean openDataBase() throws SQLException
{
String mPath = 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;
}
@Override
public synchronized void close()
{
if(myDataBase != null)
myDataBase.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase db)
{
}
@Override
public void onUpgrade(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/
Upvotes: 2