A scenario:
- add a new record with name:"name1", telephone: "123456789" --> new record
- add a new record with name:"name2", telephone:"987654321" --> update the previously entered record.
If that what you want then:
- be sure to always insert the new record with the same
id
as the previously inserted one. - use
db.insertWithOnConflict()
[ link ] to insert new records, passing the valueCONFLICT_REPLACE
[ link ] for the last parameterconflictAlgorithm
Sample Code
void Add_Contact(Person_Contact contact)
{
db = this.getWritableDatabase();
ContentValues values = new ContentValues();
// SINGLE_ROW_ID is a constant holding the single row id that will be used. e.g: SINGLE_ROW_ID = 1
values.put( KEY_ID, SINGLE_ROW_ID );
values.put( KEY_NAME, contact.get_name() ); // Contact Name
values.put( KEY_PH_NO, contact.get_phone_number()); // Contact Phone
// Inserting Row
db.insert( TABLE_CONTACTS, null, values );
db.insertWithOnConflict( TABLE_CONTACTS,KEY_ID,values, SQLiteDatabase.CONFLICT_REPLACE );
db.close(); // Closing database connection
}