2015-04-02 50 views
0

我想添加或更新Android中的SQLite数据库中的信息。 数据库获取课程笔记ID和学生ID。如果课文标识为0,则会创建一个新条目。Android SQLite更新/插入数据不会工作

这里是什么在我的Dbhelper类(插入,更新是相当类似):

public boolean insertLessonNotes(LessonNotesData lData) 
{ 
    SQLiteDatabase db = this.getWritableDatabase(); 
    ContentValues lessonValues = new ContentValues(); 
    //values.put(LESSON_ID, lData.getLessonID()); // Lesson ID 
    //values.put(ST_ID, lData.getStudentID()); // Student ID 
    //values.put(LESSON_IMAGE_ID, lData.getImageID()); // Lesson Image ID 
    //values.put(LESSON_IMAGE_NOTE, lData.getImageNote()); // Note attached to image 
    lessonValues.put(LESSON_ID, lData.getLessonID()); 
    lessonValues.put(ST_ID, lData.getStudentID()); 
    lessonValues.put(DATE, lData.getDate()); // Date note added 
    lessonValues.put(LESSON_READING, lData.getReadingNote()); // reading notes 
    lessonValues.put(LESSON_PHONICS, lData.getPhonicsNote()); // phoncis notes 
    lessonValues.put(LESSON_SPELLING, lData.getSpellingNote()); // spelling notes 
    lessonValues.put(LESSON_WRITING, lData.getWritingNote()); // writing notes 
    lessonValues.put(LESSON_COMMENTS, lData.getCommetns()); // comments notes 
    lessonValues.put(LESSON_HOMEWORK, lData.getHomework()); // homework notes 
    // Inserting Row 
    db.insert(TABLE_LESSON_NOTES, null, lessonValues); 
    db.close(); 
    return true; 
} 

这是我lessonNotes类(其中输入和保存的信息)

LessonNotesData lNote = new LessonNotesData(id_To_Update, student_id, lessonDate.getText().toString(), readNote.getText().toString(), phonNote.getText().toString(), spellNote.getText().toString(), writeNote.getText().toString(), commentNote.getText().toString(), homeworkNote.getText().toString()); 
    Bundle extras = getIntent().getExtras(); 
    int studentID = extras.getInt("studentId"); 
    int lessonID = extras.getInt("lessonID"); 
    Toast.makeText(getApplicationContext(), "LessonID is:(lessonNotes3) "+String.valueOf(lessonID), Toast.LENGTH_SHORT).show(); 
    Toast.makeText(getApplicationContext(), "StudentID is: (lessonNotes3) "+String.valueOf(studentID), Toast.LENGTH_SHORT).show(); 
    //Bundle studentId = getIntent().getExtras(); 
    if (lessonID != 0) { 
     if (studentID > 0) { 
      if (mydb.updateLessonNotes(lNote)) { 
       //Update was successful 
       Toast.makeText(getApplicationContext(), "Updated successfully", Toast.LENGTH_SHORT).show(); 
       Intent intent = new Intent(getApplicationContext(), com.example.ltss.dyslexia.app.LessonNotesList.class); 
       intent.putExtra("studentId",studentID); 
       startActivity(intent); 
      } else { 
       //Update did not complete correctly 
       Toast.makeText(getApplicationContext(), "Update unsuccessful, Please try again", Toast.LENGTH_SHORT).show(); 
      } 
     } else { 

      if (mydb.insertLessonNotes(lNote)) { 
       Toast.makeText(getApplicationContext(), "New Lesson note created", Toast.LENGTH_SHORT).show(); 
      } else { 
       Toast.makeText(getApplicationContext(), "Error creating lesson note! Please try again", Toast.LENGTH_SHORT).show(); 
      } 
      //Return user to lessonNotesList where ListView should now show new lesson note 
      Intent intent = new Intent(getApplicationContext(), com.example.ltss.dyslexia.app.LessonNotesList.class); 
      intent.putExtra("studentId",studentID); 
      startActivity(intent); 
     } 
    } 

每当我尝试添加或更新系统中的任何东西,我得到这个从调试日志:

04-02 20:02:33.925 26789-26789/com.example.ltssdyslexiaapp D/Items to add:﹕ [email protected] 

关于这是什么或我如何解决它的任何想法?

感谢

+0

如果**总是返回true **,那么**布尔**函数的用途是什么?你可以发布你的'表格创建'代码吗? – 2015-04-03 07:55:48

回答

0

其实它不是在所有它只是一个Log输出错误,它是一个DEBUG类型的日志。你colud尝试登录相同的添加行android.util.Log.d(“hello”,“world”);到任何你想要的方法。而'[email protected]'表示LessonNotesData没有toString()方法,这就是为什么你看到@ 42602bf8 - 它是一个在内存或散列中的实例地址。

+0

是的,我知道这是一个日志:) 我不知道为什么它打印出来的消息调试日志,当我还没有实际得到我的代码中的日志语句打印出'项目添加... ' 另外,我仍然不知道为什么它不会将信息添加到系统中?任何想法,我可能做错了什么? – ShWhite 2015-04-02 21:56:46

+0

也许该日志输出来自您在项目中使用的某个第三方库。你这个输出不一定是由SQLite产生的,也可能是在把对象保存到数据库的时候产生的。它可以来自任何Collection类(如果使用Guava?),或者来自某个ListView,如果您使用的是新的gogle的RecyclerView。您也可以在AS/IDEA项目的根文件夹中执行整个项目搜索:Ctrl + Shift + F - 您是否以这种方式搜索Log.d? – Stan 2015-04-06 09:35:01

+0

另外,如果你正在使用Loaders从DB获取数据到ListView中,这个输出可能来自那里,因为你在DB Loader中添加一条新记录可能会自动更新以将已存储的项添加到列表/游标中。 – Stan 2015-04-06 09:48:36