먼저 데이터베이스에 레코드가 없으면 데이터를 삽입합니다. 하지만 새 데이터가 서버에서 올 때 서버 레코드가 데이터베이스에 존재하는지 확인하고 싶습니다. 데이터베이스에서 모든 레코드를 가져오고 해시 맵 루프를 사용합니다. 레코드가 업데이트 레코드보다 많으면 그렇지 않습니다. 그렇지 않으면 새 레코드를 삽입하십시오.값을 확인하는 방법은 안드로이드의 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);
}
여전히 문제가있어 도움을 – Mahesh
를 사용하여 삽입합니다 기록을 중복 다른 상태로 가고있다 – Mahesh
id를 모두 인쇄 해보십시오. –