2012-04-10 98 views
2

Android系統是否自動關閉打開的SQLite數據庫?我已經實現了一個用於訪問數據的單例類,有時候這些方法需要在工作線程上運行。我有與在後臺運行一個查詢功能同步,這裏的代碼:Android:FC查詢sqlite時打開數據庫

public ArrayList<ActualCall> getUnsentCalls(boolean forUnsentSignature) { 
    ArrayList<ActualCall> calls = new ArrayList<ActualCall>(); 

    Cursor c; 
    open(); 

    if (forUnsentSignature) 
     c = db.rawQuery("SELECT * FROM " + TXN_ACTUAL_CALL_COMPONENT 
       + " WHERE sentFlag=1 and sigSentFlag=?", new String[] {"0"}); 
    else 
     c = db.rawQuery("SELECT * FROM " + TXN_ACTUAL_CALL_COMPONENT 
       + " WHERE sentFlag=? AND signature != ''", new String[] {"0"}); 

    if (c.moveToFirst()) { 
     do { 
      if (!db.isOpen()) 
       open(); 
      ActualCall call = new ActualCall(); 

      call.setAcpComponentId(c.getString(c.getColumnIndex("acpComponentId"))); 
      call.setAcpDate(c.getString(c.getColumnIndex("acpDate"))); 
      call.setAcpId(c.getString(c.getColumnIndex("acpId"))); 
      call.setCallEndTime(c.getString(c.getColumnIndex("callEndTime"))); 
      call.setCallStartTime(c.getString(c.getColumnIndex("callStartTime"))); 
      call.setCoordinates(c.getString(c.getColumnIndex("coordinates"))); 
      call.setDeviceTimestamp(c.getString(c.getColumnIndex("deviceTimestamp"))); 
      call.setDoctorId(c.getString(c.getColumnIndex("doctorId"))); 
      call.setSignature(c.getString(c.getColumnIndex("signature"))); 
      call.setSigSentFlag(c.getString(c.getColumnIndex("sigSentFlag"))); 
      call.setTimeSpent(c.getString(c.getColumnIndex("timeSpent"))); 
      call.setSentFlag(c.getString(c.getColumnIndex("sentFlag"))); 
      call.setCoordinates(c.getString(c.getColumnIndex("coordinates"))); 

      calls.add(call); 

     } while (c.moveToNext()); 
    } 

    c.close(); 
    db.close(); 

    return calls; 
} 

正如你所看到的,我已經叫open()的初始行還我收到講述了一個錯誤我的數據庫已經關閉。這裏的堆棧跟蹤:

java.lang.IllegalStateException: database  
/data/data/com.***.****/databases/****.db (conn# 0) already closed 
at android.database.sqlite.SQLiteDatabase.verifyDbIsOpen(SQLiteDatabase.java:2123) 
at android.database.sqlite.SQLiteDatabase.lock(SQLiteDatabase.java:398) 
at android.database.sqlite.SQLiteDatabase.lock(SQLiteDatabase.java:390) 
at android.database.sqlite.SQLiteQuery.fillWindow(SQLiteQuery.java:74) 
at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:311) 
at android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:292) 
at android.database.AbstractCursor.moveToPosition(AbstractCursor.java:156) 
at android.database.AbstractCursor.moveToFirst(AbstractCursor.java:233) 
at com.***.****.datalayer.ESCAndroidDataLayer.getUnsentCalls(ESCAndroidDataLayer.java:5340) 
at com.***.****.synchall.SynchAll$UnsentSignatureSender.run(SynchAll.java:1750) 

再次,上述方法調用一個Thread類,如果有幫助。我發現關於SQLite的鏈接被自動關閉,但我不明白他的解決方案:http://darutk-oboegaki.blogspot.com/2011/03/sqlitedatabase-is-closed-automatically.html

回答

0

嘗試調用db.acquireReference() c.moveToFirst()

db.acquireReference(); 

    if (c.moveToFirst()) { 

或之前如果(db.isOpen())「簡單地將db.close()用」「括起來就足夠了。

if (db.isOpen()) 
    { 
     db.close(); 
    } 

我不太確定,但這些會改變一些東西。

+0

我有一個位置獲取工作的背景,它提取一個位置,然後將其保存到數據庫。我不確定如果這可能會影響我的其他數據庫訪問,如果它巧合同時發生。 – 2013-06-07 01:55:21