0

データベースに2つのテーブルを作成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()でのみ閉じることができます。

編集:デザインパターンについてはこれを参照できます。

EDIT 2 :データベースを一度だけ開き、決して閉じずに問題が解決するかどうかを確認することで、これが問題であるかどうかを確認できます。そうであれば、以前の編集でリンクされた記事を参照してください。

于 2013-03-28T17:58:15.547 に答える