数据库自行关闭

时间:2012-12-28 09:12:37

标签: java android sqlite

这是第一次发生,一旦我完成执行查询,数据库连接就会关闭。在下面的代码中,在我尝试执行更多查询的第一个循环之后,我收到一个数据库连接已经关闭的错误。如果我再次手动打开数据库,那么代码运行正常,即查询执行没有任何错误。

为什么我不这样做时数据库会被关闭?

openDB()和closeDB()是打开和关闭数据库的连接。

代码:http://pastie.org/5587804#32,34

public void insertCheckedValuesInTable(int test_id,
            ArrayList<ArrayList<String>> checkedValues) {
        // TODO Auto-generated method stub
        openDB();
        // first collect all the values then insert
        // insert test_id, Q_id , correct answer and submitted answer in table
        // testAnswerSubmitted

        for (int i = 0; i < checkedValues.size(); i++) {
            int Q_id = getQ_id(checkedValues.get(i).get(0).toString().trim());
            String correctAns = getCorrectAnswer(
                    test_id,
                    Integer.parseInt(checkedValues.get(i).get(0).toString()
                            .trim()));
            String submittedAns = checkedValues.get(i).get(1).toString().trim();
            int marksObt;
            Log.e("value " + i, correctAns.equalsIgnoreCase(submittedAns) + "");
            // get marks
            if (correctAns.equalsIgnoreCase(submittedAns)) {
                // = marks for this question;
                Cursor cur = myDataBase.rawQuery(
                        "(SELECT Marks FROM testQuestionBank WHERE Test_id ="
                                + test_id + " AND Q_id = " + Q_id + ")", null);
                cur.moveToFirst();
                marksObt = Integer.parseInt(cur.getString(0));
                cur.close();
            } else {
                marksObt = 0;
            }
            // done collecting all values, insert in db now

            Log.e("database state", myDataBase.isOpen() + "");

            openDB();

            String sql = "Insert INTO testAnswerSubmitted (Test_id, Q_id, CorrectAnswer, SubmittedAnswer, MarksObtained) VALUES ( "
                    + test_id
                    + ", '"
                    + Q_id
                    + "', '"
                    + correctAns
                    + "', '"
                    + submittedAns + "', '" + marksObt + "') ";
            myDataBase.execSQL(sql);
            Log.v("Executed", sql);
        }
        closeDB();
    }

openDB():

public void openDB() {
        try {
            createDataBase();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            Log.e("DataBaseHelper", e.toString());
        }

        openDataBase();
        getWritableDatabase();

    }

其中createDatabase()是:

public void createDataBase() throws IOException {

        boolean dbExist = checkDataBase();

        if (dbExist) {
            // do nothing - database already exist
        } else {

            // By calling this method and empty database will be created into
            // the default system path
            // of your application so we are gonna be able to overwrite that
            // database with our database.
            // this.getReadableDatabase(); // changed to write
            this.getWritableDatabase();

            try {

                copyDataBase();

            } catch (IOException e) {

                throw new Error("Error copying database");

            }
        }

    }

closeDB():

@Override
    public synchronized void close() {

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

        super.close();

    }

0 个答案:

没有答案