mardi 29 mars 2016

How to Insert and update in sqlite android

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("lawyer_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());
            }

        }

Aucun commentaire:

Enregistrer un commentaire