Reputation: 87
I never worked in database.But I read sqlite databse and manage to create table and how to insert in the table.But I want to know now how to update in database.As,Id which I am using is autoincrement so,how to update using autoincrement Id.
Will anybody help me.As,I am learning things in android.
Any help is appreciated.Thanks.
Upvotes: 0
Views: 112
Reputation: 18923
Make Function in your Database class:
public void updateData(String str1, String str2) {
// TODO Auto-generated method stub
ContentValues cvUpdate = new ContentValues();
cvUpdate.put("KEY name or id here", str1);
yourDatabase.update(DATABASE_TABLE, cvUpdate, KEY name or id here+ "='"
+ str1+ "'", null);
}
Upvotes: 1
Reputation: 4089
sdb=openOrCreateDatabase("sample.db", Context.MODE_WORLD_WRITEABLE, null);
ContentValues cv= new ContentValues();
cv.put("col1","string1");
cv.put("col2","string2");
sdb.update("table", cv, "col_name = ?", new String[] {"col_name_value"});
sdb.close();
here col_name
is an auto increment column
Upvotes: 0
Reputation: 6444
Update example
// Updating single contact
public int updateContact(Contact contact) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, contact.getName());
values.put(KEY_PH_NO, contact.getPhoneNumber());
// updating row
return db.update(TABLE_CONTACTS, values, KEY_ID + " = ?",
new String[] { String.valueOf(contact.getID()) });
}
Upvotes: 0