Frage

I have created a table for my application... first time user will give the input for two editText ,name and mobile number when table is empty after that only updating the first row of table...so on

War es hilfreich?

Lösung

A scenario:

  1. add a new record with name:"name1", telephone: "123456789" --> new record
  2. add a new record with name:"name2", telephone:"987654321" --> update the previously entered record.

If that what you want then:

  1. be sure to always insert the new record with the same id as the previously inserted one.
  2. use db.insertWithOnConflict()[ link ] to insert new records, passing the value CONFLICT_REPLACE [ link ] for the last parameter conflictAlgorithm

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 
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top