我在我的数据库中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
。请告诉我我哪里错了。谢谢