2012-10-24 4 views
2

Possible Duplicate:
Android error - close() was never explicitly called on database는 내 안드로이드 응용 프로그램에서이 문제를

했다 "close()를 명시 적으로의 SQLiteDatabase에 데이터베이스에 호출되지 않았습니다."

나는 싱글이 다음 코드를 사용하는 방법이있는 구현 : SQLiteDatabase: close() was never explicitly called on database

방법이 문제를 방지하려면 :이 사용할 때

public Cursor getFooCursor(Context context) 
{ 
    StorageDBOpenHelper helper = new StorageDBOpenHelper(context); 
    SQLiteDatabase db = helper.getReadableDatabase(); 

    Cursor c = db.query("Foo", new String[] {"_id", "Titel"}, null, null, null, null, "Test DESC"); 

    return c; 
} 

, 난 가끔 오류를? 문제는, 간단히 말해서 db.close()return c으로 비울 수 없기 때문입니다.

답변

2

I이 사용하는 접근 방식을 클래스에 db의 인스턴스를 전달하는 반환하는 cursor :

StorageDBOpenHelper helper = new StorageDBOpenHelper(context); 
SQLiteDatabase db = helper.getReadableDatabase(); 

public Cursor getFooCursor(Context context, SQLiteDatabase db) { 
     Cursor c = db.query("Foo", new String[] {"_id", "Titel"}, null, null, null, 
null, "Test DESC"); 
     return c; 
} 

db.close(); 
2

클라이언트는 데이터베이스를 열고이 방법을 사용하여 커서를 가져온 다음 커서와 데이터베이스가 모두 끝나면 닫습니다. 여기에 싱글 톤을 사용하지 않는 것이 좋습니다. 대신 같은 것을 할 :

public class FooDB 
{ 
    private SQLiteDatabase db = null; 

    private void open() throws SQLiteException 
    { 
     if (db != null) 
     { 
      throw new SQLiteException("Database already opened"); 
     } 

     // Create our open helper 
     StorageDBOpenHelper helper = new StorageDBOpenHelper(context); 
     try 
     { 
      // Try to actually get the database objects 
      db = m_openHelper.getWritableDatabase(); 
     } 
     catch (Exception e) 
     { 
      e.printStackTrace(); 
     } 

     if (db == null) 
     { 
      throw new SQLiteException("Failed to open database"); 
     } 
    } 

    private void close() throws SQLiteException 
    { 
     if (db != null) 
     { 
      db.close(); 
      db = null; 
     }   
    } 

    public Cursor getFooCursor(Context context) 
    { 
     if(db == null) 
      throw new SQLiteException("Database not open");  

     Cursor c = db.query("Foo", new String[] {"_id", "Titel"}, null, null, null, null, "Test DESC"); 

     return c; 
    } 
} 
관련 문제