"无法打开数据库文件"使用SQLiteOpenHelper与仪器上下文时 [英] "Unable to open database file" when using SQLiteOpenHelper with instrumentation context

查看:118
本文介绍了"无法打开数据库文件"使用SQLiteOpenHelper与仪器上下文时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个包含用于测试目的模仿对象的数据库。我想preFER有这个数据库驻留在我的测试包中,而不是在我的应用程序包。

I'm trying to create a database that contains mock objects for testing purposes. I'd prefer to have this db reside in my test package instead of in my application package.

protected class MockObjectOpenHelper extends SQLiteOpenHelper {

    MockObjectOpenHelper() {
        super(instrumentation.getContext(), FILE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        ...
    }
}

当我创造我SQLiteOpenHelper从我的应用程序,我通过在仪器仪表方面还有一个路径是相对于我的测试包的数据库目录。我认为这可以让我创造我的测试包的数据库,但是这似乎并不如此。在数据库目录在我的测试包永远不会创建。

When I create my SQLiteOpenHelper from within my app, I pass in the instrumentation context as well as a path that's relative to my test package's database directory. I would think this would allow me to create a database in my test package, but this doesn't seem to be the case. The "databases" directory is never created in my test package.

有没有办法让我的应用程序创建和访问驻留在我的测试项目中的数据库?

Is there a way to have my app create and access a database that resides in my test project?

例外情况:

14:50:13.917 pool-1-thread-1 android.database.sqlite.SQLiteException: unable to open database file
at android.database.sqlite.SQLiteDatabase.dbopen(Native Method)
at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1584)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:638)
at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:659)
at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:652)
at android.app.ApplicationContext.openOrCreateDatabase(ApplicationContext.java:482)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:98)
at com.myapp.test.http.DbMockHttpClient.createClientRequestDirector(DbMockHttpClient.java:52)
at com.myapp.test.http.UniversalMockHttpClient.createClientRequestDirector(UniversalMockHttpClient.java:42)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:539)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
E

推荐答案

我有同样的问题。使用应用程序上下文没有的伎俩我:

解决方案
I had the same issue. Using the application context did the trick for me:

而不是

instrumentation.getContext()

这篇关于&QUOT;无法打开数据库文件&QUOT;使用SQLiteOpenHelper与仪器上下文时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆