1

我正在尝试更新我的数据库。它并没有真正更新。我认为这是由于我的更新命令

我的主要活动是列表视图活动。我在操作栏上有一个按钮,它基本上用两个编辑框启动一个新活动。因此,一旦单击该按钮,我就会创建一个新的数据库项。它最初设置为两个空字符串。在我的第二个活动中,我有一个保存按钮,应该将我的更改保存到文本框中

所以在我的主要活动将我带到我的第二个活动之后,代码如下

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.note);

    // if new then create new db, else retrieve old database, right now
    // assume everything is new

    db = new DBHandler(this);
    nowAdd = new UENote("", "");
    db.addNote(nowAdd);
}

我通过此功能更新我的 nowAdd 联系人字符串

    public void updateColumnData() {
    // gets the strings from edittext
    EditText textTitle = (EditText) findViewById(R.id.editText1);
    EditText textSubject = (EditText) findViewById(R.id.editText2);

    // converts the string
    String titleString = textTitle.getText().toString();
    String subjectString = textSubject.getText().toString();

    nowAdd.setSubject(subjectString);
    nowAdd.setTitle(titleString);

    db.updateNote(nowAdd);

}

更新笔记功能如下

    public int updateNote(UENote note) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_TITLE, note.getTitle());
    values.put(KEY_SUBJECT, note.getSubject());

    // updating row
    return db.update(TABLE1, values, KEY_ID + " = ? ",
            new String[] { String.valueOf(note.getID()) });
}
4

1 回答 1

3

确保您传入的iddb.update()与您要更新的 id 完全相同。从代码片段中,您如何获得note.getID(). 您是否正在查询空记录以获取id

假设这KEY_ID是一个auto incremented primary key,然后我会调试以检查它是否返回您要更新的记录getID()的相同ID 。

于 2013-09-01T22:49:53.303 回答