I've been away from java for a while and as a warmup, decided to run a basic database/list-keeping app to help me keep track of things I need as I move. I'm largely finished with the app, it runs roughly but well enough for my purposes, but I'm having a lot of issues with the SQLite database functions. The only one I've yet to sort out is the "update" function.
Here is my current code, within my SQLITE Helper class:
public int updateObject(HomeObject originalObject, HomeObject updatedObject) {
// 1. get reference to writable DB
SQLiteDatabase db = this.getWritableDatabase();
// 2. create ContentValues to add key "column"/value
ContentValues values = new ContentValues();
values.put("name", updatedObject.getName());
values.put("room", updatedObject.getRoom()); // get title
values.put("have", updatedObject.getHave()); // get author
// 3. updating row
int i = db.update(TABLE_HomeObjects, //table
values, // column/value
KEY_ID+" = "+String.valueOf(originalObject.getId()), // selections
null ); //selection args
// 4. close
db.close();
return i;
}
However, when I use the modify button which is meant to call this method and update the object correspondingly, it appears to simply delete the object. It disappears off of all lists and in-app searches reveal it to be no longer in the database.
Here's what the relevant code I use to call it looks like:
MySQLiteHelper db = new MySQLiteHelper(this);
Intent intent = getIntent();
String itemName = intent.getStringExtra(LivingRoom.EXTRA_MESSAGE);
// Redacted... There is a large section of code here
// which deals with pulling the newlyEditedName, newlyEditedHave,
// and newlyEditedRoom variables from different places in the view.
HomeObject newlyEditedObject = new HomeObject(newlyEditedName, newlyEditedHave, newlyEditedRoom);
HomeObject originalObject = db.getObject(itemName);
int test = db.updateObject(originalObject, newlyEditedObject);
I'm very new to SQLite, so maybe I'm missing something basic... But what's going wrong here? The log reveals that the method is returning "1". As another side-note, I can confirm that the "getObject" method works perfectly, as it gets used multiple times before this, so I know it's returning correctly.
Thank you for any assistance! If I can get this worked out, I can actually start using this thing.
0 comments:
Post a Comment