0

我在我的数据库中weekone创建了两个表。weektwo他们都成功地上传了数据库中的数据EditTexts,但是当我想通过按下View按钮查看数据库时,应用程序崩溃了。

这就是我如何将 Editext 中的条目保存在表中的数据库中weekone

String treadmillTimings = durOnTreadmill.getText().toString();

                DatabaseManager entry = new DatabaseManager(this);
                entry.open();
                entry.createEntry(treadmillTimings);
                entry.close();

                String stepperTimings = durOnStepper.getText().toString();

                DatabaseManager entry1 = new DatabaseManager(this);
                entry1.open();
                entry1.week1createEntry1(stepperTimings);
                entry1.close();

                String stationaryRowingTimings = durOnStationaryRowing.getText().toString();

                DatabaseManager entry2 = new DatabaseManager(this);
                entry2.open();
                entry2.week1createEntry2(stationaryRowingTimings);
                entry2.close();

                String exerciseBikeTimings = durOnExerciseBike.getText().toString();

                DatabaseManager entry3 = new DatabaseManager(this);
                entry3.open();
                entry3.week1createEntry3(exerciseBikeTimings);
                entry3.close();

                String ellipticalTrainerTimings = durOnEllipticalTrainer.getText().toString();

                DatabaseManager entry4 = new DatabaseManager(this);
                entry4.open();
                entry4.week1createEntry4(ellipticalTrainerTimings);
                entry4.close();

在表中写入条目weekone

//creating entry in table for treadmill in table week 1 with the help of ContentValues

public long createEntry(String treadmillTimings) 
{
    // TODO Auto-generated method stub

    ContentValues cv = new ContentValues();

    //enterting each exercise name corresponding to their respective edit Texts

    cv.put(KEY_EXERCISENAME, "Treadmill");
    cv.put(KEY_DURATION, treadmillTimings);

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

}

//creating entry in table for stepperTimings in table week 1 with the help of ContentValues

public long week1createEntry1 (String stepperTimings)
{
    ContentValues cv1 = new ContentValues();
    cv1.put(KEY_EXERCISENAME, "Stepper");
    cv1.put(KEY_DURATION, stepperTimings);
    return ourDatabase.insert(DATABASE_TABLE, null,cv1);

}

//creating entry in table for Stationary Rowing in table week 1 with the help of ContentValues

public long week1createEntry2 (String stationaryRowingTimings)
{
    ContentValues cv2 = new ContentValues();
    cv2.put(KEY_EXERCISENAME, "Stationary Rowing");
    cv2.put(KEY_DURATION, stationaryRowingTimings);

    return ourDatabase.insert(DATABASE_TABLE, null,cv2);

}

//creating entry in table for exercise bike in table week 1 with the help of ContentValues

public long week1createEntry3 (String exerciseBikeTimings)
{
    ContentValues cv3 = new ContentValues();
    cv3.put(KEY_EXERCISENAME, "Exercise Bike");
    cv3.put(KEY_DURATION, exerciseBikeTimings);
    return ourDatabase.insert(DATABASE_TABLE, null,cv3);

}

//creating entry in table for elliptical trainer in table week 1 with the help of ContentValues

public long week1createEntry4 (String ellipticalTrainerTimings)
{
    ContentValues cv4 = new ContentValues();
    cv4.put(KEY_EXERCISENAME, "Stationary Rowing");
    cv4.put(KEY_DURATION, ellipticalTrainerTimings);
    return ourDatabase.insert(DATABASE_TABLE, null,cv4);

}

显示数据库中的条目

//displaying/reading data in the table using cursor

public String week1getData() 
{
    // TODO Auto-generated method stub
    String[] columns = new String[]{ KEY_ROWID, KEY_EXERCISENAME, KEY_DURATION};
    Cursor cur = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);

    //creating a result(string type variable) to store the text and display it.

    String result = "";

    int iRow = cur.getColumnIndex(KEY_ROWID);
    int iExerciseName = cur.getColumnIndex(KEY_EXERCISENAME);
    int iDuration = cur.getColumnIndex(KEY_DURATION);

    // cursor start from the first position, keeps moving to the next as long as the position in not after that last.

    for(cur.moveToFirst(); !cur.isAfterLast(); cur.moveToNext())
    {
        /*getting the rows, exercise name and duration in the tables of database and setting it to result. 
          .The next time it loops, it will still have the prevoius result*/

        result = result + cur.getString(iRow) + "              " + cur.getString(iExerciseName) + "                            " + cur.getString(iDuration) + "\n";
    }

    return result;
}

表的所​​有代码都是相同的,weektwo除了下面的

public String week2getData()      <------- ERROR IS IN THIS METHOD, BASED ON LOGCAT
{
    // TODO Auto-generated method stub
    String[] columns = new String[]{ KEY_ROWID, KEY_EXERCISENAME, KEY_DURATION};
    Cursor cur = ourDatabase.query(DATABASE_TABLE2, columns, null, null, null, null, null);

    //creating a result(string type variable) to store the text and display it.

    String result = "";

    int iRow = cur.getColumnIndex(KEY_ROWID);
    int iExerciseName = cur.getColumnIndex(KEY_EXERCISENAME);
    int iDuration = cur.getColumnIndex(KEY_DURATION);

    // cursor start from the first position, keeps moving to the next as long as the position in not after that last.

    for(cur.moveToFirst(); !cur.isAfterLast(); cur.moveToNext())
    {
        /*getting the rows, exercise name and duration in the tables of database and setting it to result. 
          .The next time it loops, it will still have the previous result*/

        result = result + cur.getString(iRow) + "              " + cur.getString(iExerciseName) + "                            " + cur.getString(iDuration) + "\n";
    }

    return result;


}

weektwo另外,我对我所做的一切都做了同样的事情weekone。请告诉我我哪里错了。谢谢

4

1 回答 1

0

看到您的 logcat,当您尝试读取数据时,您的数据库似乎已关闭。我想建议不要那么频繁地打开和关闭数据库。这会在代码中造成很多混乱。您可以尝试在父 Activity(您传递的上下文)的 onCreate 处打开数据库,然后仅在 onDestroy() 中将其关闭。

编辑:您可以参考设计模式。

编辑2:您可以通过仅打开数据库一次并且从不关闭它以查看问题是否消失来确认这是否是问题。如果确实如此,请参阅先前编辑中的链接文章。

于 2013-03-28T17:58:15.547 回答