2013-03-28 3 views
0

데이터베이스에 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); 

} 

표시 항목에 데이터베이스에 Editext에서 항목을 저장하고 어떻게

입니다

//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; 
} 

모든 테이블에 대한 코드가 동일합니다. 아래

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; 


} 

를 제외한 88,983,210는 Addtionally 난 내가 weekone 위해 한 어떤 weektwo에 대한 동일했다. 내가 어디가 잘못 됐는지 말해줘. 감사합니다

+0

logcat을 올리시겠습니까? 어떤 구체적인 오류가 발생했는지보고 싶습니다. –

+0

매우 길기 때문에 http://cat.bin.com/k48xdSr9에서 logcat을 확인하십시오. 또한, week2getData 메소드는 DatabaseManager.java에 있습니다. – user2179825

+0

추락 한 지점을 excatly 알려주십시오. 디버깅을 시도하고 데이터베이스 뷰를 열 때 – Coder

답변

0

귀하의 logcat을 봤는데 데이터를 읽으려고하면 데이터베이스가 닫힌 것 같습니다. 나는 데이터베이스를 너무 자주 열거 나 닫지 말 것을 제안하고 싶다. 이것은 코드에 많은 혼란을 야기합니다. 상위 작업 (전달 된 컨텍스트)의 onCreate에서 데이터베이스를 열고 onDestroy()에서만 닫을 수 있습니다.

편집 : 디자인 패턴에 대해서는 this을 참조 할 수 있습니다.

EDIT 2 : 데이터베이스가 한 번만 열리고 문제가 없는지 확인하기 위해 데이터베이스를 한 번만 열어 문제가 있는지 여부를 확인할 수 있습니다. 그것이 있으면 이전 편집에서 링크 된 기사를 참조하십시오.

+0

부분적으로 작동합니다. 즉, 그것은 나에게 양쪽 테이블 엔리를 하나씩 보여 주지만, 엔리 티를 별도의 테이블에 보여주기 위해서 필요합니다. – user2179825

관련 문제