1

我想将 idsqlite的值与TAG_ID来自 json webservice 的值进行比较。我只是坚持这样做..

到目前为止我尝试过的是

class LoadAll extends AsyncTask<String, String, String> 
    {

            protected String doInBackground(String... args) 
            {
                Log.i("url ",url_all_products);

                    List<NameValuePair> params = new ArrayList<NameValuePair>();
                    JSONObject json = jParser.makeHttpRequest(url_all_products, "GET",params);
                    Log.w("All book: ", json.toString());
                    Log.i(url_all_products, url_all_products);                     
                    try 
                    {
                        System.err.println("json array "+json.getJSONArray(TAG_PRODUCTS));
                        bookProduct = json.getJSONArray(TAG_PRODUCTS);

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

                    } 
                    if(data_exist!=bookProduct.length()){
                        Log.i("in update","m here");
                         Cursor cursors = getRawEvents("select id from bcuk_book");

                             try{

                             for (int i = 0; i < bookProduct.length(); i++) {
                                 JSONObject c = bookProduct.getJSONObject(i);

                                 String Bid = c.getString(TAG_ID);

                                 ArrayList<String> mapId = new ArrayList<String>();

                                    mapId.add(TAG_ID);

                                    Log.e(Bid,Bid);

                                    while(cursors.moveToNext())
                                    {
                                     System.err.println("update coded STEON"+cursors.getString(0));
                                     Log.e(Bid,c.getString(TAG_ID));

                                    }
                             }

                         }
                             catch(JSONException e){
                                 e.printStackTrace();
                             }
                    }
                    else{
                        Log.i("Done good","m here");
                    }
                    return null;               
            }

背后的原因

    if(data_exist!=bookProduct.length()){

这个语句是变量data_exist=cursor.getCount();

并且bookProduct.length是jsonarray的长度如果长度不同我想比较它们然后我想将新值更新到sqlite中。任何人都可以建议我这样做的方法

4

1 回答 1

1

如果长度不同,那么我想将新值更新到 sqlite。

不是更新数据库的好方法。
假设服务器数据库字段发生了一些变化。数量相同,但内容不同。
这就是我的做法:

 String sql = "select * from " + DB_TABLE + " where " + TAG_ID + "='" + id + "'";
 Cursor c = database.rawQuery(sql, null);
 c.moveToFirst();
 if( c.getCount() > 0){ // already in the database
      // update entry with TAG_ID = id
  }
  else{ // new entry
      // add new entry
  }
于 2012-12-17T09:57:47.623 回答