這是我的SQLiteHeler.java類。android.database.sqlite.DatabaseObjectNotClosedException:應用程序未關閉在此處打開的遊標或數據庫對象
//tables name in database
public static final String TABLE_NAME="Favtable";
//common column for all table
public static final String KEY_ID="id";
//column in alumnitable
public static final String KEY_CID="Courseid";
public static final String KEY_FavStatus="Status";
public SQLiteHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
//create tables
@Override
public void onCreate(SQLiteDatabase database){
String CREATE_NAME="CREATE TABLE IF NOT EXISTS "+TABLE_NAME+" ("+KEY_ID+" INTEGER PRIMARY KEY,"+KEY_CID+" VARCHAR, "+KEY_FavStatus+" VARCHAR)";
database.execSQL(CREATE_NAME);
database.close();
}
public void InsertData(int Courseid,int flag)
{
Cursor cursor = null;
SQLiteDatabase database = this.getReadableDatabase();
cursor.moveToFirst();
SQLiteQuery = "INSERT INTO FavTable (Courseid,Status) VALUES('" + Courseid + "', '" + flag + "');";
database.execSQL(SQLiteQuery);
database.close();
}
public void close() {
if (database != null) {
database.close();
}
}
@Override
public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
database.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
onCreate(database);
}
}
什麼,我做錯了什麼?它給錯誤的
_android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here_
我打電話創建和創建對象插入SQLiteHelper的方式進入我的片段類。
這是我CourseFragment.java.here我打電話的onCreate和插入方法
alertDialogBuilder.setPositiveButton( 「是」, 新DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface arg0,
int arg1) {
flag = 1;
/*s1="true";*/
Courseid = mCourse.getCourseid();
mCourse.setIsFavCourse(!mCourse.getIsFavCourse());
mCourse.save();
// Update listview
mCourses.get(position).setIsFavCourse(
mCourse.getIsFavCourse());
courseListAdapter.notifyDataSetChanged();
//Add courseid and status to database
favourite.onCreate(database);
favourite.InsertData(Courseid,flag);
// Start sync service for course
Intent i = new Intent(context, MDroidService.class);
i.putExtra("notifications", false);
i.putExtra("siteid",
session.getCurrentSiteId());
i.putExtra("courseid",
mCourse.getCourseid());
context.startService(i);
}
});
入住這http://stackoverflow.com/questions/4734886/how-can-one-avoid-databaseobjectnotclosedexception – Raghavendra
'我做錯了什麼?'很多事情。 ** 1 ** - 您將**整數**值作爲**串**插入。 ** 2 ** - 你正在創建一個**無用的**遊標,然後它被移動到第一個記錄(但它沒有關聯到任何記錄集),它仍然是** null **。 ** 3 ** - 你永遠不會關閉它。 –
'遊標cursor = null'和'cursor.moveToFirst'沒有用處,後者可能會導致問題。並不是說這兩條線會導致這個問題。如果通過說_I叫做create_你叫'onCreate'那個方法被自動調用,所以你不應該直接調用它。 – MikeT