如何在sqlite android中插入和更新

时间:2016-03-29 12:34:57

标签: android sqlite

我从服务器获取数据并将其存储在本地数据库中,即sqlite。它在sqlite中插入重复的值。

所以我的条件是:

  1. 检查sqlite db为空,插入记录
  2. 检查已存在的sqlite的id列,如果是,请更新它,否则插入。
  3. 我这样做了: -

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

0 个答案:

没有答案