我在本教程中设置了一个SQLite数据库,将其更改为我自己的项目:http : //www.androidhive.info/2011/11/android-sqlite-database-tutorial/
但是,本教程没有提到如何更新字段。
db = new NotesDatabaseHandler(ViewNoteActivity.this);
...
db.updateNote(new Note(identifier, editTextTitle.getText().toString(), editTextContent.getText().toString(), "Updated on: " + printStandardDate()));
虽然没有运气..我运行该应用程序,但笔记不会更新。
笔记类别:
package com.timmo.notes;
class Note {
//private variables
private int _id;
private String _title;
private String _content;
private String _metadata;
// Empty constructor
public Note() {
}
// constructor
public Note(int id, String title, String content, String metadata) {
this._id = id;
this._title = title;
this._content = content;
this._metadata = metadata;
}
public int getID() {
return this._id;
}
public void setID(int id) {
this._id = id;
}
public String getTitle() {
return this._title;
}
public void setTitle(String title) {
this._title = title;
}
public String getContent() {
return this._content;
}
public void setContent(String content) {
this._content = content;
}
public String getMetadata() {
return this._metadata;
}
public void setMetadata(String metadata) {
this._metadata = metadata;
}
}
从NotesDatabaseHandler():
// Updating single note
public int updateNote(Note note) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_TITLE, note.getTitle());
values.put(KEY_CONTENT, note.getContent());
values.put(KEY_METADATA, note.getMetadata());
// updating row
int ret = db.update(TABLE_NOTES, values, KEY_ID + " = ?",
new String[]{String.valueOf(note.getID())});
db.close();
return ret;
}
以前,我已经删除了便笺,然后又将其添加了,但这会将其添加到数据库的末尾,而不是放在同一位置。
因此,对于这个问题,如何从使用的方法中正确更新我的笔记(联系人)之一?谢谢
更新:我尝试使用原始的更新:
public void updateNote(Note note) {
SQLiteDatabase db = this.getWritableDatabase();
String strSQL = "UPDATE "
+ TABLE_NOTES + " SET "
+ KEY_TITLE + "='" + note.getTitle() + "', "
+ KEY_CONTENT + "='" + note.getContent() + "', "
+ KEY_METADATA + "='" + note.getMetadata() + "'"
+ " WHERE " + KEY_ID + "='" + note.getID() + "'";
Log.d("SQL: ", strSQL);
db.execSQL(strSQL);
db.close();
}
然而仍然没有任何反应。我的日志输出:
UPDATE notes SET title='hello', content='there', metadata='Updated on: 09:48 AM - 08 Aug 2015' WHERE id='7'
我看不出有什么问题...
我设法解决了我的问题。@Tomasz的最佳指针。
固定更新注:
public void updateNote(Note note) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_TITLE, note.getTitle());
values.put(KEY_CONTENT, note.getContent());
values.put(KEY_METADATA, note.getMetadata());
db.update(TABLE_NOTES, values, KEY_ID + " = ?",
new String[]{String.valueOf(note.getID())});
db.close();
}
调用语句:
db.updateNote(new Note(identifier, editTextTitle.getText().toString(), editTextContent.getText().toString(), "Updated on: " + printStandardDate()));
这些以及其他修复程序在尝试更新RecyclerView项目时出现了一个问题,而我最终修复或创建了解决方法。该应用程序现在可以正常运行,并且我已经向商店发布了更新:https : //play.google.com/store/apps/details?id=com.timmo.notes
我应该尽快将源代码发布到github,并添加指向描述的链接。
再次感谢所有提供帮助的人!
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句