Prachi Karapurkar Prachi Karapurkar - 9 months ago 27
Android Question

how to insert in sqlite checking if entry exits and if exists update it and if not present then insert

This is my create function which inserts array data in database.Data is fetching from server and inserting in db.

public long createEmployee_New(ArrayList<Customer> aCustomer) {

long row_id = 0;
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();

for(int i = 0;i<aCustomer.size();i++) {

Customer employee = new Customer();
employee = aCustomer.get(i);
values.put(EMP_ID, employee.getEmpID());
values.put(EMP_TYPE, employee.getEmpType());
values.put(EMP_CITY, employee.getEmpCity());
values.put(EMP_NAME, employee.getEmpName());
values.put(EMP_CONTACT_PERSON, employee.getEmpContactPersonName());
values.put(EMP_CONTACT, employee.getEmpContactNo());
values.put(EMP_ADD, employee.getEmpAddress());
values.put(EMP_STATE, employee.getEmpState());
values.put(EMP_DELSTATE, employee.getEmpDelState());
values.put(CUSTOMER_LANDLINE, employee.getCustomer_landline());
values.put(CUST_COMPANY_ID, employee.getCust_companyID());

row_id = db.insert(TABLE_EMPLOYEE, null, values);

return row_id;

how can i check if empid which is unique exits in table if exists then update the data for that particular empid and if its not present in db then create


For Updating and Inserting data you can also Use below method

mDatabase.insertWithOnConflict(TABLE_EMPLOYEE, null, values,