android - How to replace the values in table while the insert name already exist in the table -


how update values while insert if exist same name in table. example: table have values(bala,100,chennai).again try insert values if inserting name same want replace values(bala,100,chennai) (bala,50,mumbai). while insert(bala,50,mumbai). beginner in database side. in advance.

public class horizantaldbhandler extends sqliteopenhelper {  // static variables // database version private static final int database_version = 1;  // database name private static final string database_name = "hrlist";  // contacts table name // private static final string table_contacts = "breakfast"; private static final string table_breakfast = "hrlist";  // contacts table columns names  private static final string key_id = "id"; private static final string key_number = "number"; private static final string key_name = "name"; private static final string key_image = "image";  public horizantaldbhandler(context context) {     super(context, database_name, null, database_version); }  // creating tables @override public void oncreate(sqlitedatabase db) {     string create_breakfast_table = "create table " + table_breakfast + "("             + key_id + " integer primary key," + key_name + " text,"             + key_number + " text," + key_image + " blob" + ")";      db.execsql(create_breakfast_table); }  // upgrading database @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) {     // drop older table if existed     db.execsql("drop table if exists " + table_breakfast);      // create tables again     oncreate(db); }  /**  * crud(create, read, update, delete) operations  */  public// adding new contact void addcontact(horizantalpojo contact) {     sqlitedatabase db = this.getwritabledatabase();      contentvalues values = new contentvalues();      system.out.println("################");     system.out.println("################");     system.out.println("################");     system.out.println(contact._name);     system.out.println(contact._number);     system.out.println(contact._image);      values.put(key_name, contact._name); // contact name     values.put(key_number, contact._number);     values.put(key_image, contact._image); // contact phone     //     //     // inserting row     db.insert(table_breakfast, null, values);      system.out.println("inserted successsssssssss");     db.close(); // closing database connection     // } } public list<horizantalpojo> getallcontacts() {     list<horizantalpojo> contactlist = new arraylist<horizantalpojo>();     // select query     string selectquery = "select  * hrlist order name";      sqlitedatabase db = this.getwritabledatabase();     cursor cursor = db.rawquery(selectquery, null);     // looping through rows , adding list     if (cursor.movetofirst()) {         {             horizantalpojo contact = new horizantalpojo();             contact.setid(integer.parseint(cursor.getstring(0)));             contact.setname(cursor.getstring(1));             contact.setnumber(cursor.getstring(2));             contact.setimage(cursor.getblob(3));              system.out.println("^^^^^^^^^^^^^^^^");             system.out.println("^^^^^^^^^^^^^^^^");             system.out.println("^^^^^^^^^^^^^^^^");             system.out.println("^^^^^^^^^^^^^^^^");             system.out.println(cursor.getstring(0));             system.out.println(cursor.getstring(1));             system.out.println(cursor.getstring(2));             system.out.println(cursor.getblob(3));              // adding contact list             contactlist.add(contact);         } while (cursor.movetonext());     }     // close inserting data database     db.close();     // return contact list     return contactlist;  } } 

how replace values in table while insert name exist in table

need use update method instead of insert updating data in existing row:

1. use select query check if record present or not:

cursor cursor = db.rawquery("select * " + table_breakfast + "                               "+key_name+"= '" +contact._name + "'" , null); if(cursor!=null){   if(cursor.getcount()>0){      // use db.update updating current columns values       contentvalues values = new contentvalues();       values.put(key_number, contact._number);       values.put(key_image, contact._image);       db.update(table_breakfast, values,key_name +                              " = ?"+contact._name, new string[] {contact._name});   }else{      // use db.insert add new record in db   } } 

Comments

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -