使用后续查询时无法打开数据库文件错误

时间:2015-03-27 18:22:28

标签: android sqlite cursor

我有以下代码,第一个游标对象工作正常,但是当我执行另一个查询并将其分配给flightCursor时,它会给出错误。

Cursor cursor = database.query( CityAndAirportsTable.notificationsTable, new String[] { CityAndAirportsTable.notifyFlightId },
                null, null, null, null, "Id DESC" );
        cursor.moveToFirst();
        while( !cursor.isAfterLast() ){
            String id = String.valueOf( cursor.getInt( 0 ) );
            Cursor flightCursor = database.query( CityAndAirportsTable.flightTable, new String[] { CityAndAirportsTable.fromDestinationCode,
            CityAndAirportsTable.toDestinationCode, CityAndAirportsTable.currentPrice }, CityAndAirportsTable.flightId + "=" + id,
                    null, null, null, null );
}

这里的flightCursor = database.query,我收到了错误。

日志

03-27 23:49:09.628: E/SQLiteLog(2296): (14) cannot open file at line 30046 of [9491ba7d73]
03-27 23:49:09.628: E/SQLiteLog(2296): (14) os_unix.c:30046: (24) open(/data/data/com.flightapp.myapp/databases/Application_DB-journal) - 
03-27 23:49:09.628: E/SQLiteLog(2296): (14) cannot open file at line 30046 of [9491ba7d73]
03-27 23:49:09.628: E/SQLiteLog(2296): (14) os_unix.c:30046: (24) open(/data/data/com.flightapp.myapp/databases/Application_DB-journal) - 
03-27 23:49:09.628: E/SQLiteLog(2296): (14) statement aborts at 11: [SELECT From_Destination_Code, To_Destination_Code, Current_Price FROM Flights WHERE Id=2] unable to open database file
03-27 23:49:09.629: E/SQLiteQuery(2296): exception: unable to open database file (code 14); query: SELECT From_Destination_Code, To_Destination_Code, Current_Price FROM Flights WHERE Id=2

stackoverflow上有类似的问题,但在我的情况下,第一个查询有效,但第二个查询失败。

1 个答案:

答案 0 :(得分:13)

完成后关闭光标!我认为你打开的光标对象太多了

Cursor cursor = database.query( CityAndAirportsTable.notificationsTable, new String[] { CityAndAirportsTable.notifyFlightId },
                null, null, null, null, "Id DESC" );
cursor.moveToFirst();
while( !cursor.isAfterLast() ){
  String id = String.valueOf( cursor.getInt( 0 ) );
  Cursor flightCursor = database.query(
    CityAndAirportsTable.flightTable,
    new String[] { CityAndAirportsTable.fromDestinationCode,
      CityAndAirportsTable.toDestinationCode,
      CityAndAirportsTable.currentPrice },
    CityAndAirportsTable.flightId + "=" + id,
    null, null, null, null );

  /* Close the cursor here! */
  flightCursor.close();
  /* ---------------------- */
}

希望这可以解决您的问题