Q:

Unable to open database file error on using subsequent queries

I have the following code, the first cursor object works fine, but when i do another query and assign it to the flightCursor, it gives the error.
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 );
}

Here in the flightCursor = database.query, i get the error.
Logs
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

There are similar question on stackoverflow, but in my case the first query works but the second one fails.

A:

Close your cursor when you're done with it! I think you've just got too many cursor objects open
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();
  /* ---------------------- */
}

Hopefully this fixes your issue

