Android Test =打开已经关闭的数据库

时间:2012-07-23 15:56:48

标签: android sqlite testing

我创建了一个复制已经预制的数据库的应用程序,然后允许您使用SQLTest作为其主要活动来运行SQLiteDatabase(插入,更新等)的各种查询/方法。然后我做了一个测试(称为SQLTestCase,我知道这是不合适的,但我是新手测试),它有一个测试套件调用randomTest()方法随机选择要执行的测试。奇怪的是我收到一个错误说:

java.lang.IllegalStateException: attempt to re-open an already-closed object:        SQLiteDatabase: /data/data/com.example.sql2/databases/os.sqlite
at android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:55)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1310)
at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1253)
at com.example.sql2.test.SQLTestCase.randomTest(SQLTestCase.java:236)
at com.example.sql2.test.SQLTestCase.testSuite(SQLTestCase.java:38)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:214)
at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:199)
at android.test.ActivityInstrumentationTestCase2.runTest(ActivityInstrumentationTestCase2.java:192)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:190)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:175)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1584)

错误显示在两行代码中:

public void testSuite()
{
    int length = 100;
    for (int x = 0; x < length; x++)
    {
        randomTest(); // Here
    }
}

public void randomTest()
{
    ...
    String sql = "SELECT COUNT(*) AS NumberOfRecords FROM" + SQLAdapter.TABLE_OS;
    Cursor cursor = activity.accessAdapter().accessHelper().accessDatabase().rawQuery(sql, null); //Here
            ...

我知道问题在于游标的初始化,因为通过注释,测试运行正常。有谁知道这个问题?我查看了我的代码,希望找到一个我尽早关闭数据库的地方,但我什么都没看到。我会尽力提供你需要的东西。如果有更多代码可以解决这个问题,请告诉我。

public class SQLTestCase extends ActivityInstrumentationTestCase2<SQLTest>
{
SQLTest activity;

public SQLTestCase()
{
    super(SQLTest.class);
}

protected void setUp() throws Exception
{
    super.setUp();
    setActivityInitialTouchMode(false);
    activity = getActivity();
}

    ...

public void testSuite()
{
    int length = 100;
    for (int x = 0; x < length; x++)
    {
        randomTest();
    }
}

    ...
public void randomTest()
{
    String name = "", field = "";
    Random rand = new Random();
    int randOp = rand.nextInt(5);
    int randName, randField, id;
    String sql = "SELECT COUNT(*) AS NumberOfRecords FROM" + SQLAdapter.TABLE_OS;
    Cursor cursor = activity.accessAdapter().accessHelper().accessDatabase().rawQuery(sql, null);

    switch (randOp)
    {
    case 0:
        randName = rand.nextInt(45);
        name = getRandomName(randName);
        int nullNotNull = rand.nextInt(2);
        switch (nullNotNull)
        {
        case 0:
            field = null;
            break;
        case 1:
            randField = rand.nextInt(24);
            field = getRandomField(randField);
            break;
        }
        testInsert(name, field);
        break;
    case 1:
        id = rand.nextInt(Integer.parseInt(cursor.getString(0))); 
        //id = rand.nextInt(12)+1;
        testDelete(id);
        break;
    case 2:
        int selection = rand.nextInt(5);
        switch (selection)
        {
        case 0:
            randName = rand.nextInt(45);
            id = rand.nextInt(Integer.parseInt(cursor.getString(0)));
            //id = rand.nextInt(12)+1;
            name = getRandomName(randName);
            testUpdate(id, name, "");
            break;
        default:
            randName = rand.nextInt(45);
            name = getRandomName(randName);
            randField = rand.nextInt(24);
            field = getRandomField(randField);
            id = rand.nextInt(Integer.parseInt(cursor.getString(0)));
            //id = rand.nextInt(12)+1;
            testUpdate(id, name, field);
            break;
        }
        break;
    case 3:
        id = rand.nextInt(Integer.parseInt(cursor.getString(0)));
        //id = rand.nextInt(12)+1;
        testGet(id);
        break;
    case 4:
        testGetAll();
        break;
    }
}

}


public class SQLTest extends Activity
{
public SQLAdapter adapter;

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sqltest);
    adapter = new SQLAdapter(this);
    adapter.createDatabase();
    open();
}
...
}

2 个答案:

答案 0 :(得分:2)

好吧,多亏了Joel,我最终为randomTest()方法创建了一个本地数据库,然后调用了原始查询:

String sql = "SELECT COUNT(*) AS NumberOfRecords FROM " + SQLAdapter.TABLE_OS;
SQLiteDatabase db = activity.accessAdapter().accessHelper().getWritableDatabase();
Cursor cursor = db.rawQuery(sql, null);

这很有效(虽然我后来意识到这对我的项目来说没有用)。无论哪种方式,如果其他人在使用这样的游标时遇到问题,这就是适合我的解决方案。

答案 1 :(得分:0)

不知道这是否有用,但每次完成时都需要关闭光标。尝试拨打cursor.close();