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

4
我有以下代码,第一个 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个回答

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();
  /* ---------------------- */
}

希望这能解决您的问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接