android.database.sqlite.DatabaseObjectNotClosedException:应用程序未关闭此处打开的游标或数据库对象

时间:2016-11-15 07:54:41

标签: android sqlite android-sqlite

这是我的SQLiteHeler.java类。

//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_

我通过创建Object来调用SQLiteHelper的创建和插入方法到我的Fragment类中。

这是我的CourseFragment.java.here我正在调用onCreate和Insert Method

alertDialogBu​​ilder.setPositiveButton("是&#34 ;,                                     新的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);

                                    }
                                });

1 个答案:

答案 0 :(得分:0)

复制并运行代码然后修改它。主要/初始问题是由于您在帮助程序的 onCreate 方法中创建数据库时关闭数据库(在下面的代码中注释掉)。

但是,根据评论,还有其他问题,其中一些问题可以解决。以下代码有效(至于解决您目前的问题): -

public class SQLiteHelper extends SQLiteOpenHelper {

    public static final String DATABASE_NAME = "Courses";

    //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)
    {
        SQLiteDatabase database = this.getWritableDatabase();
        String SQLiteQuery = "INSERT INTO FavTable (Courseid,Status) VALUES('" + Courseid + "', '" + flag + "');";
        database.execSQL(SQLiteQuery);
    }


    @Override
    public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
        database.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
        onCreate(database);
    }
}

以下是我在活动中使用的代码/调用: -

    SQLiteHelper mydb = new SQLiteHelper(this);
    mydb.InsertData(0,10);
    mydb.close();

你的代码中不应该包含这一行(在我回答之后发布): -

favourite.onCreate(database);