2014-06-18 3 views
0

먼저 데이터베이스에 레코드가 없으면 데이터를 삽입합니다. 하지만 새 데이터가 서버에서 올 때 서버 레코드가 데이터베이스에 존재하는지 확인하고 싶습니다. 데이터베이스에서 모든 레코드를 가져오고 해시 맵 루프를 사용합니다. 레코드가 업데이트 레코드보다 많으면 그렇지 않습니다. 그렇지 않으면 새 레코드를 삽입하십시오.값을 확인하는 방법은 안드로이드의 ArrayList <Hashmap <String, String >>에 존재합니까?

여기

난 내 코드를 삽입하면 확인할 수 있습니다 그것은

String data=loadJSONFromAsset(); 
      Log.e("data","--->"+data); 
      try 
      { 

       Open_Database(); 

       JSONObject jobject= new JSONObject(data); 
       JSONArray jstore= jobject.getJSONArray(WS_Constant.GETALLSSID.STORETERMINALINFO); 

       myDBwifilist=mDB_Helper.GetAllRecord(mSQLiteDatabase,DB_Constant.TABLE.MYWIFI); 
       Log.d("Record Count","----->"+myDBwifilist.size()); 

       // json array must > 0 
       if(jstore.length()>0) 
       { 

        for(int i=0;i<jstore.length();i++) 
        { 

         if(myDBwifilist.size()<=0) 
         { 
           mDB_Helper.Insert_MYWIFI_Table(mSQLiteDatabase, 
                jstore.getJSONObject(i).getString(WS_Constant.GETALLSSID.WIFINAME), 
                jstore.getJSONObject(i).getString(WS_Constant.GETALLSSID.TERMINAL_ID), 
                jstore.getJSONObject(i).getString(WS_Constant.GETALLSSID.STORE_ID), 
                jstore.getJSONObject(i).getString(WS_Constant.GETALLSSID.LONGITUDE), 
                jstore.getJSONObject(i).getString(WS_Constant.GETALLSSID.LATITUDE)); 
         } 
         else 
         { 

           for (HashMap<String, String> map : myDBwifilist) 
           { 
             for (String key : map.keySet()) 
             { 
               if (key.equals(DB_Constant.MYWIFI.TERMINAL_ID)) 
               { 
                if(map.get(key).equals(jstore.getJSONObject(i).getString(WS_Constant.GETALLSSID.TERMINAL_ID))) 
                { 
                 int check=mDB_Helper.UpdateALLSSID(mSQLiteDatabase, 
                   jstore.getJSONObject(i).getString(WS_Constant.GETALLSSID.TERMINAL_ID), 
                   jstore.getJSONObject(i).getString(WS_Constant.GETALLSSID.WIFINAME), 
                   jstore.getJSONObject(i).getString(WS_Constant.GETALLSSID.STORE_ID), 
                   jstore.getJSONObject(i).getString(WS_Constant.GETALLSSID.LONGITUDE), 
                   jstore.getJSONObject(i).getString(WS_Constant.GETALLSSID.LATITUDE)); 
                 Log.d("Record Update", "----------> True"+check); 
                } 
                else 
                { 
                 mDB_Helper.Insert_MYWIFI_Table(mSQLiteDatabase, 
                    jstore.getJSONObject(i).getString(WS_Constant.GETALLSSID.WIFINAME), 
                    jstore.getJSONObject(i).getString(WS_Constant.GETALLSSID.TERMINAL_ID), 
                    jstore.getJSONObject(i).getString(WS_Constant.GETALLSSID.STORE_ID), 
                    jstore.getJSONObject(i).getString(WS_Constant.GETALLSSID.LONGITUDE), 
                    jstore.getJSONObject(i).getString(WS_Constant.GETALLSSID.LATITUDE)); 

                 Log.d("Record Update", "----------> false"); 
                } 
               } 
             } 
           } 

         } 

        } 
       } 
       else 
       { 


       } 

       Close_Database(); 

      } 
      catch (JSONException e) 
      { 
       e.printStackTrace(); 
      } 

// 가져 오기 데이터베이스에서 레코드

public ArrayList<HashMap<String,String>> GetAllRecord(SQLiteDatabase db, String TableName) 
     { 

      ArrayList<HashMap<String, String>> data = new ArrayList<HashMap<String,String>>(); 

      String Query = "SELECT * FROM " + TableName; 
      Cursor cursor = db.rawQuery(Query, null); 
      if(cursor.getCount()<=0) 
      { 
         cursor.close(); 
         return data; 
      } 
      else 
      { 
       if (cursor.moveToFirst()) 
       { 
        do 
        { 
         HashMap<String, String> map = new HashMap<String, String>(); 
         map.put(DB_Constant.MYWIFI.WIFINAME, cursor.getString(cursor.getColumnIndex(DB_Constant.MYWIFI.WIFINAME))); 
         map.put(DB_Constant.MYWIFI.TERMINAL_ID, cursor.getString(cursor.getColumnIndex(DB_Constant.MYWIFI.TERMINAL_ID))); 
         map.put(DB_Constant.MYWIFI.STORE_ID, cursor.getString(cursor.getColumnIndex(DB_Constant.MYWIFI.STORE_ID))); 
         map.put(DB_Constant.MYWIFI.LONGITUDE, cursor.getString(cursor.getColumnIndex(DB_Constant.MYWIFI.LONGITUDE))); 
         map.put(DB_Constant.MYWIFI.LATITUDE, cursor.getString(cursor.getColumnIndex(DB_Constant.MYWIFI.LATITUDE))); 
         data.add(map); 

        } while (cursor.moveToNext()); 

       } 
      }  
      cursor.close(); 
      return data; 
     } 

public int UpdateALLSSID(SQLiteDatabase db,String terminalid, 
          String wifiname, 
          String storeid, 
          String longitude, 
          String latitude) 
     { 


      Log.e("terminalid","--->"+terminalid); 

      ContentValues cv = new ContentValues(); 
      cv.put(DB_Constant.MYWIFI.WIFINAME,wifiname); 
      cv.put(DB_Constant.MYWIFI.STORE_ID,storeid); 
      cv.put(DB_Constant.MYWIFI.LONGITUDE,longitude); 
      cv.put(DB_Constant.MYWIFI.LATITUDE,latitude); 
      return db.update(DB_Constant.TABLE.MYWIFI, cv,DB_Constant.MYWIFI.TERMINAL_ID +" = "+terminalid, null); 


     } 

답변

2
Try this way,hope this will help you to solve your problem. 

     boolean isNew=true; 
     for (HashMap<String, String> map : myDBwifilist){ 
      if(map.get(DB_Constant.MYWIFI.TERMINAL_ID).equals(jstore.getJSONObject(i).getString(WS_Constant.GETALLSSID.TERMINAL_ID)) && map.get(DB_Constant.MYWIFI.WIFINAME).equals(jstore.getJSONObject(i).getString(WS_Constant.GETALLSSID.WIFINAME))){ 
       isNew = false; 
       break; 
      }else{ 
       isNew = true; 
      } 
     } 

     if(isNew){ 
      mDB_Helper.Insert_MYWIFI_Table(mSQLiteDatabase, 
        jstore.getJSONObject(i).getString(WS_Constant.GETALLSSID.WIFINAME), 
        jstore.getJSONObject(i).getString(WS_Constant.GETALLSSID.TERMINAL_ID), 
        jstore.getJSONObject(i).getString(WS_Constant.GETALLSSID.STORE_ID), 
        jstore.getJSONObject(i).getString(WS_Constant.GETALLSSID.LONGITUDE), 
        jstore.getJSONObject(i).getString(WS_Constant.GETALLSSID.LATITUDE)); 

      Log.d("Record Update", "----------> false"); 
     }else{ 
      int check=mDB_Helper.UpdateALLSSID(mSQLiteDatabase, 
        jstore.getJSONObject(i).getString(WS_Constant.GETALLSSID.TERMINAL_ID), 
        jstore.getJSONObject(i).getString(WS_Constant.GETALLSSID.WIFINAME), 
        jstore.getJSONObject(i).getString(WS_Constant.GETALLSSID.STORE_ID), 
        jstore.getJSONObject(i).getString(WS_Constant.GETALLSSID.LONGITUDE), 
        jstore.getJSONObject(i).getString(WS_Constant.GETALLSSID.LATITUDE)); 
      Log.d("Record Update", "----------> True"+check); 
     }  
+0

여전히 문제가있어 도움을 – Mahesh

+0

를 사용하여 삽입합니다 기록을 중복 다른 상태로 가고있다 – Mahesh

+0

id를 모두 인쇄 해보십시오. –

0

그 다음 삽입 다른 업데이트하거나 true 인 경우 값이 HashMap

HashMap<String, String> hMap = new HashMap<String, String>(); 
hMap.put("1", "One"); 
hMap.put("2", "Two"); 
hMap.put("3", "Three"); 

System.out.println(hMap.containsValue("Two")); // the output will be true. 

에 있는지 여부를 일반 예는 확인합니다. 필요에 따라 코드를 수정하십시오.

+0

내가 존재하지 않는 경우 만에 새 레코드를 삽입 할 내가 왜 다른 – Mahesh

+0

감사를 정확히 원하는 작동하지 그렇지 않으면 레코드가 존재하면 상태가됩니다. – Mahesh

관련 문제