0

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

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user3415586
  • 137
  • 1
  • 1
  • 8
  • I am having trouble understanding what you are asking. Do you want to save and update ONE user's name and mobile number, or simply create a new user with name and mobile number everytime? – AndyH Apr 09 '14 at 07:07

1 Answers1

1

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 
}
kdehairy
  • 2,630
  • 22
  • 27
  • hi attached my code make me clear public void Add_Contact(Person_Contact contact) { db = this.getWritableDatabase(); ContentValues values = new ContentValues(); 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,1); db.close(); // Closing database connection } – user3415586 Apr 10 '14 at 10:31
  • I edited my answer. check it and let me know if you need more clarification :) – kdehairy Apr 10 '14 at 15:07
  • thank ......you so much...i am new to android i will keep on asking doubt help me – user3415586 Apr 16 '14 at 06:25
  • Thanks this worked for me. @kdehairy please do you think you could give me your opinion on this question http://stackoverflow.com/questions/25598696/recommended-way-order-to-read-data-from-a-webservice-parse-that-data-and-inse – Axel Sep 02 '14 at 03:19