Hi all I want to update a row on clicking on update button,but its doesn’t work.
I have used following code.
public void btnUpdate(View v) {
handeler.updateData(updateName.getText().toString(), updatePhone .getText().toString(), updateEmail.getText().toString(),id);
}
public void updateData(String name, String phone, String email, String id) {
ContentValues values = new ContentValues();
values.put(COLUMN_FIRST, name);
values.put(COLUMN_SECOND, phone); values.put(COLUMN_THIRD, email); database.update(TABLE_NAME, values, id, null);
}
public void search() {
Cursor cursor = handeler.getData();
if (cursor.moveToFirst()) {
String phoneNo;
phoneNo = updateByPhone.getText().toString();
do {
String s1 = cursor.getString(2);
if (phoneNo.compareTo(s1) == 0) {
id = cursor.getString(0);
updateName.setText(cursor.getString(1));
updateEmail.setText(cursor.getString(3));
updatePhone.setText(cursor.getString(2));
}
} while (cursor.moveToNext());
}
}
So if any know please suggest me how to solve it.
Thanks
I see a couple possible issues:
1) You have an extra space
updatePhone .getText().toString()should beupdatePhone.getText().toString()2) you are passing a variable
idfrombtnUpdatetoupdateDatabut it is not clear where it is coming from (or even if it actually exists)My bet is that #2 is your issue. You probably need to pass the id (I assume that’s meant to be the RowId you want to modify in the db) in to the
btnUpdatemethod:There are other possibilities… you haven’t shown your DB structure, so it could be that some constraint is causing the update to fail.
EDIT
The
update methoddocs show this:Note the part
String whereClause. That’s supposed to be a SQL WHERE statement (without the WHERE). You are only passing in the id, not a WHERE clause. Change your call to update to make that a WHERE clause and it should work. A couple of examples:Both examples assume your row id column is labeled _id.
The first example is if
idis an integer value. The issue there is that you are settingidas a string, so I’m unsure if this is the case or not. If it is supposed to be an int, you should get it usingid = cursor.getInt(0);instead ofid = cursor.getString(0);.If it truly is a string and not an int, use the second version, which encloses
idin single quotes to indicate it is a string.Hope this helps!