我在数据库中创建了两个表weekone
和weektwo
。他们都是从EditTexts
中成功地将数据上传到数据库中,但是当我想按View
按钮查看数据库时,应用程序崩溃了。
这是我在表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
做了完全相同的事情。请告诉我哪里出错了。感谢
答案 0 :(得分:0)
看到您的logcat,当您尝试读取数据时,似乎您的数据库已关闭。我想建议不要频繁地打开和关闭数据库。这在代码中造成了很多混乱。您可以尝试在父Activity的onCreate(您传递的上下文)中打开数据库,并仅在onDestroy()中关闭它。
编辑:您可以参考this了解设计模式。
编辑2:您可以通过仅打开一次数据库并且从不关闭它来查看问题是否消失来确认这是否是问题。如果是,请参考之前编辑中的链接文章。