2011-12-15 4 views
1

데이터베이스를 닫을 때 데이터베이스를 만들 수 없습니다 (아래의 코드를 확인하십시오). 만약 내가 그것을 닫지 않으면 모든 것이 잘 작동하지만 logcat에 몇 가지 오류가 발생하여 close()이 명시 적으로 호출되지 않는다고 말합니다. 데이터베이스를 만든 다음 close()을 추가하면 오류가 사라지 겠지만 내 앱을 다시 설치하고 실행할 수는 없습니다. 데이터베이스가 올바르게 닫혔습니다.

private static class DbHelper extends SQLiteOpenHelper { 

    public DbHelper(Context context) { 
     super(context, DATABASE_score, null, DATABASE_VERSION); 
     // TODO Auto-generated constructor stub 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 
     // TODO Auto-generated method stub 

     db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ROWID 
       + " INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE, " + KEY_SCORE1 
       + " INTEGER TEXT NOT NULL, " + KEY_SCORE2 
       + " INTEGER TEXT NOT NULL);"); 
     db.close(); <---problem 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     // TODO Auto-generated method stub 
     db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE); 
     onCreate(db); 
    } 
} 

public Database(Context c) { 
    ourContext = c; 
} 

public Database open() throws SQLException { 
    ourHelper = new DbHelper(ourContext); 
    ourDatabase = ourHelper.getWritableDatabase(); 
    return this; 
} 

public void close() { 
    ourHelper.close(); 
} 

public long createEntry(String score, String score2) { 
    // TODO Auto-generated method stub 
    ContentValues cv = new ContentValues(); 
    cv.put(KEY_SCORE1, score); 
    cv.put(KEY_SCORE2, score2); 

    return ourDatabase.insert(DATABASE_TABLE, null, cv); 
} 

그래서 질문은 : 어떻게 db.close()를 호출하고 내 응용 프로그램이 제대로에서 실행해야합니까?)()

+1

적어도 10-15 줄의 logcat 출력을 게시하십시오. –

답변

2

당신은 당신의 DbHelper 클래스에 onCreate() 내부 db.close();를 호출 할 필요가 없습니다.

이 대신 당신이 DbHelper_instance.getReadableDatabase() 또는 DbHelper_instance.getWritableDatabase()를 사용하여 DbHelper 클래스에서 데이터베이스 인스턴스를 획득하고 SQLiteDatabase 객체에 그를 할당해야 (의는의 SQLiteDatabase 데이터베이스로 정의 할 수 있습니다). 데이터베이스를 사용하여 완료, 당신은 커서를 닫은 후

, 당신은 같은 것을 수행 할 수 있습니다

 if (database.isOpen()) { 
      Log.d(TAG + " Closing SQLite Database connection..."); 
      database.close(); 
     } 

을 당신은 항상 염두에 복용 (실행됩니다 확실하게 차단 마침내를 넣어 수 귀하의 DB 작업은 try/catch로 둘러싸여 있습니다).

+0

하게/ –

+0

는 당신이 직접 질문이있는 경우, 도움, 또는 게시 할 수 있습니다 전체 소스. – hovanessyan

+0

슬픈 일은 내가했지만 정말 정말 밝은 사람이 내 게시물을 편집했습니다 !!!! 귀하의 게시물에 논리를보고 그것을 구현하려고합니다 .... –

0
private Context context; 
private SQLiteDatabase db; 

public DataBase(Context context) { 
    this.context = context; 
    OpenHelper openHelper = new OpenHelper(this.context); 
    this.db = openHelper.getWritableDatabase(); 
} 

public void closeData() { 
    this.db.close(); 
} 

이 시도;

if(myDataBase != null) 
     myDataBase.close(); 

     super.close(); 
+0

흠 캔트 \t private Context context; \t \t private SQLiteDatabase db; \t \t private Database dbHelper; \t \t public DbHelper(Context context) { \t \t \t super(context, DATABASE_score, null, DATABASE_VERSION); \t \t \t // TODO Auto-generated constructor stub \t \t \t this.context = context; \t \t \t this.db = dbHelper.getWritableDatabase(); \t \t } \t \t public void close() { \t \t \t dbHelper.close(); \t \t }

0

전화 super.close을,

+0

desnt 작업 .... 그게 충돌 작업을 진행/내가 당신의 솔루션을 구현하지 못할 어떤 이유로 응용 프로그램 충돌 –

관련 문제