我有一个 SQLite 数据库,它可以工作,但是在更改或添加竞争时在 Logcat 上出现以下错误。我必须关闭数据库吗?
06-08 12:52:29.897: E/Database(6274): close() was never explicitly called on database '/data/data/de.android.test/databases/table.db'
06-08 12:52:29.897: E/Database(6274): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
06-08 12:52:29.897: E/Database(6274): at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1900)
06-08 12:52:29.897: E/Database(6274): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:884)
06-08 12:52:29.897: E/Database(6274): at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:918)
06-08 12:52:29.897: E/Database(6274): at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:911)
06-08 12:52:29.897: E/Database(6274): at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:548)
06-08 12:52:29.897: E/Database(6274): at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203)
06-08 12:52:29.897: E/Database(6274): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:98)
06-08 12:52:29.897: E/Database(6274): at de.database.DAO.<init>(DAO.java:39)
06-08 12:52:29.897: E/Database(6274): at de.AWocheActivity.onClick(AWocheActivity.java:551)
06-08 12:52:29.897: E/Database(6274): at android.view.View.performClick(View.java:2408)
06-08 12:52:29.897: E/Database(6274): at android.view.View$PerformClick.run(View.java:8817)
06-08 12:52:29.897: E/Database(6274): at android.os.Handler.handleCallback(Handler.java:587)
06-08 12:52:29.897: E/Database(6274): at android.os.Handler.dispatchMessage(Handler.java:92)
06-08 12:52:29.897: E/Database(6274): at android.os.Looper.loop(Looper.java:144)
06-08 12:52:29.897: E/Database(6274): at android.app.ActivityThread.main(ActivityThread.java:4937)
06-08 12:52:29.897: E/Database(6274): at java.lang.reflect.Method.invokeNative(Native Method)
06-08 12:52:29.897: E/Database(6274): at java.lang.reflect.Method.invoke(Method.java:521)
06-08 12:52:29.897: E/Database(6274): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
06-08 12:52:29.897: E/Database(6274): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
06-08 12:52:29.897: E/Database(6274): at dalvik.system.NativeStart.main(Native Method)
这是代码:
活动:
public void onCreate(Bundle savedInstanceState) {
.
.
.
dao = new DAO(this);
Cursor subjectList = dao.fetchAllSubject();
}
public void onClick(View v) {
global_db_Id = 0;
dao = new DAO(this);
Cursor subjectList = dao.fetchAllSubject();
if (global_db_Id == 0){
long insertedId = dao.createSubject(String.valueOf(hour), "A", String.valueOf(day), fach_kurz.getText().toString(), fach_lang.getText().toString(), String.valueOf(item), room.getText().toString(),String.valueOf(index_awoche));
}else{
dao.updateSubject(String.valueOf(global_db_Id), String.valueOf(hour), "A", String.valueOf(day), fach_kurz.getText().toString(), fach_lang.getText().toString(), String.valueOf(item), room.getText().toString(), String.valueOf(index_awoche));
//if (dao != null) {
//dao.close();
//}
}
在 onClick 方法结束时,我尝试关闭数据库,但随后应用程序崩溃了。数据库:
public class DAO {
private DatabaseHelper dbHelper;
private SQLiteDatabase database;
public long createSubject(String hour, String week, String day, String name,String desc, String col, String room, String btn_nr){
ContentValues values = new ContentValues();
values.put(COLUMN_HOUR, hour);
values.put(COLUMN_WEEK, week);
values.put(COLUMN_DAY, day);
values.put(COLUMN_NAME, name);
values.put(COLUMN_DESCRIPTION, desc);
values.put(COLUMN_COLOUR, col);
values.put(COLUMN_ROOM, room);
values.put(COLUMN_BTN_NR, btn_nr);
return database.insert(TABLE, null, values);
}
public Cursor fetchAllSubject(){
Cursor mCursor = database.query(true, TABLE, new String[] {
COLUMN_ID, COLUMN_HOUR, COLUMN_WEEK, COLUMN_DAY, COLUMN_NAME, COLUMN_DESCRIPTION, COLUMN_COLOUR, COLUMN_ROOM, COLUMN_BTN_NR},null
, null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
public int updateSubject(String id, String hour, String week, String day, String name,String desc, String col, String room, String btn_nr){
ContentValues values = new ContentValues();
values.put(COLUMN_HOUR, hour);
values.put(COLUMN_WEEK, week);
values.put(COLUMN_DAY, day);
values.put(COLUMN_NAME, name);
values.put(COLUMN_DESCRIPTION, desc);
values.put(COLUMN_COLOUR, col);
values.put(COLUMN_ROOM, room);
values.put(COLUMN_BTN_NR, btn_nr);
return database.update(TABLE, values,COLUMN_ID + "=?",new String[]{id});
}
// This I tried on the end of the onClick Method
public void close(){
if(database!=null)
database.close();
}