我对Robolectric很新,如果我在这里遗漏了一些明显的东西,请提前道歉。我有一个应用程序使用SQLiteAssetHelper从assets
目录加载数据库,我想使用Robolectric框架测试该数据库。我能够获得对数据库的引用,但它似乎是空的。 SQLiteAssetHelper向日志输出“已成功打开的数据库”消息,但数据库中没有表。
编辑:以下是使用下面发布的类@ user2953017在setUp
方法中获取数据库的代码:
@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.
}
来自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
SQL异常的原因是找不到查询中指定的表名。
我很感激任何建议。
答案 0 :(得分:2)
首先将数据库从asset文件夹复制到app数据库文件夹。 这是将复制数据库的代码
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.");
}
}
另外chk这个链接..它可能对你有用 Testing SQLite database in Robolectric
试试这些:
1.从终端
删除您的数据库adb shell
cd /data/data/com.example.apploicationname/databases
rm *
并在模拟器上重新安装您的应用程序。
尝试增加模拟器的RAM ..虽然我的数据库中包含了所有数据,但是当我将模拟器的RAM从1024增加到2000时,我也遇到了同样的错误..它工作正常。
将数据库从DDMS复制到系统文件系统,然后通过sqlite浏览器打开它,检查Table是否存在。 链接sqlite浏览器 http://sourceforge.net/projects/sqlitebrowser/files/sqlitebrowser/