3

I'm fetching data from server and storing it in local db i.e sqlite. It is inserting duplicate values in sqlite.

So my condition is :

  1. Check with sqlite db is empty,insert the record
  2. Check the id column of sqlite already present, if yes update it or else insert.

I've done so for :-

SQLiteDatabase db = dbhelp.getWritableDatabase();
        ContentValues values = new ContentValues();

        String sqlq = "Select clid from client_detail";
        Cursor cccs = db.rawQuery(sqlq, null);

        if (cccs.getCount()== 0) {
            values.put("clid", queryValues.get("clid"));
            values.put("_id", queryValues.get("_id"));
            values.put("client_id", queryValues.get("client_id"));
            values.put("client_name", queryValues.get("client_name"));
            values.put("client_mobile", queryValues.get("client_mobile"));
            values.put("client_phone", queryValues.get("client_phone"));
            values.put("client_email", queryValues.get("client_email"));
            values.put("client_address", queryValues.get("client_address"));
            db.insert("client_detail", null, values);
        }else{
            if (cccs.moveToFirst()) {
                do{

                    Log.d("Client ID",""+cccs.getString(0));
                    clid.add(cccs.getString(0));

                    String temp = cccs.getString(0);
                    Log.d("Clid",temp);
                    if(temp.equals(queryValues.get("clid"))) //checking each id present in db
                    {
                        values.put("clid", queryValues.get("clid"));
                        values.put("lawyer_id", queryValues.get("_id"));
                        values.put("client_id", queryValues.get("client_id"));
                        values.put("client_name", queryValues.get("client_name"));
                        values.put("client_mobile", queryValues.get("client_mobile"));
                        values.put("client_phone", queryValues.get("client_phone"));
                        values.put("client_email", queryValues.get("client_email"));
                        values.put("client_address", queryValues.get("client_address"));
                        db.update("client_detail", values, null, null);
                    }
                    else{
                        values.put("clid", queryValues.get("clid"));
                        values.put("lawyer_id", queryValues.get("_id"));
                        values.put("client_id", queryValues.get("client_id"));
                        values.put("client_name", queryValues.get("client_name"));
                        values.put("client_mobile", queryValues.get("client_mobile"));
                        values.put("client_phone", queryValues.get("client_phone"));
                        values.put("client_email", queryValues.get("client_email"));
                        values.put("client_address", queryValues.get("client_address"));
                        db.insert("client_detail", null, values);
                    }
                }while(cccs.moveToNext());
            }

        }
Karan
  • 252
  • 1
  • 11
  • 1
    You basically want to do it in one action? You need to Insert or Update on Conflict - this question/answer should get what you want: http://stackoverflow.com/questions/13311727/android-sqlite-insert-or-update – Mark Mar 29 '16 at 12:39
  • Yes, I need to do in one action. – Karan Mar 29 '16 at 12:41
  • Currently, records are adding 3 times.. – Karan Mar 29 '16 at 13:02
  • 1
    you can do as Mark says, also if you are getting all the data everytime, you can simply delete all records from table, then insert all records from server (this case valid if you always get all data from server) and not a sync operation. – Yazan Mar 29 '16 at 13:31
  • @Yazan Yeah.. I've done the same thing,that what you said..:) Thanks ..:) – Karan Mar 30 '16 at 08:33

0 Answers0