EternalFalls
EternalFalls

Reputation: 25

updating a row of data sqlite android

I am trying to update my database. It is not really updating. I think it is due to my update command

my main activity is a listview activity. I have a button on the action bar that basically starts a new activity with two editboxes. So once I click that button, I create a new database item. It is originally set to two empty strings. On my second activity, I have a save button that is supposed to save my changes into the textboxes

so after my main activity takes me to my second activity, the code is as follows

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

i update my nowAdd contact strings through this function

    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);

}

the update note function is as follows

    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()) });
}

Upvotes: 1

Views: 3190

Answers (1)

sjdutta
sjdutta

Reputation: 376

Make sure that the id you are passing in the db.update() is really the same one that you want to update. From the code snippet, it's not obvious how you are getting the value of note.getID(). Are you making query for empty record to get the id?

Assuming that KEY_ID is an auto incremented primary key, I would then debug getID() to check if it returns the same id of the record you wanted to update.

Upvotes: 3

Related Questions