I have a SQLite database set up from this tutorial changing it around to my own project: http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/
However the tutorial does not mention how to update a field.. I have tried this:
db = new NotesDatabaseHandler(ViewNoteActivity.this);
...
db.updateNote(new Note(identifier, editTextTitle.getText().toString(), editTextContent.getText().toString(), "Updated on: " + printStandardDate()));
Although have had no luck.. I run the application and the note will not update.
Note class:
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;
}
}
From 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;
}
Previously, I have deleted the note, then added it in again, but this adds it to the end of the database instead of in the same position.
So, to the question, how do I properly update one of my notes (contacts) from the method I am using? Thanks
Update: I've tried using a raw UPDATE:
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();
}
Yet still nothing happens. My Log outputs:
UPDATE notes SET title='hello', content='there', metadata='Updated on: 09:48 AM - 08 Aug 2015' WHERE id='7'
I can't see what's wrong...