本文介绍了使用后续查询时无法打开数据库文件错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码,第一个游标对象工作正常,但当我做另一个查询并将其分配给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,我得到错误。 p>
日志
03-27 23:49:09.628:E / SQLiteLog (2296):(14)无法在[9491ba7d73]的行30046打开文件
03-27 23:49:09.628:E / SQLiteLog(2296):(14)os_unix.c:30046: (/data/data/com.flightapp.myapp/databases/Application_DB-journal) -
03-27 23:49:09.628:E / SQLiteLog(2296):(14)无法打开文件在第30046行[ 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)语句在11处中止:[SELECT FROM_Destination_Code,To_Destination_Code,Current_Price FROM Flights WHERE Id = 2]无法打开数据库文件
03-27 23:49:09.629:E / SQLiteQuery(2296):异常:无法打开数据库文件(代码14)。查询:SELECT From_Destination_Code,To_Destination_Code,Current_Price FROM Flights WHERE Id = 2
但在我的情况下,第一个查询工作,但第二个失败。
解决方案
完成后关闭光标!我想你只有太多的游标对象打开了
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.close();
/ * ---------------------- * /
}
希望这可以修复您的问题
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.
解决方案
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
这篇关于使用后续查询时无法打开数据库文件错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!