如何SQLite表的值进行比较,以在Android上的JSON标记值 [英] How to compare the value of sqlite table to the json tag value in android

查看:152
本文介绍了如何SQLite表的值进行比较,以在Android上的JSON标记值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要的id值 sqlite的比较值 TAG_ID 从JSON Web服务来。我只是坚持做这个的。

什么到目前为止,我已经尝试为

 类LOADALL扩展的AsyncTask<字符串,字符串,字符串>
    {            保护字符串doInBackground(字符串参数... args)
            {
                Log.i(URL,url_all_products);                    清单<&的NameValuePair GT; PARAMS =新的ArrayList<&的NameValuePair GT;();
                    JSONObject的JSON = jParser.makeHtt prequest(url_all_products,GET,则params);
                    Log.w(所有的书:json.toString());
                    Log.i(url_all_products,url_all_products);
                    尝试
                    {
                        通信System.err.println(JSON数组+ json.getJSONArray(TAG_PRODUCTS));
                        bookProduct = json.getJSONArray(TAG_PRODUCTS);                    }
                    赶上(JSONException E)
                    {
                            e.printStackTrace();                    }
                    如果(data_exist!= bookProduct.length()){
                        Log.i(在更新,M这里);
                         光标光标= getRawEvents(从bcuk_book选择ID);                             尝试{                             的for(int i = 0; I< bookProduct.length();我++){
                                 JSONObject的C = bookProduct.getJSONObject(I)                                 字符串买入= c.getString(TAG_ID);                                 ArrayList的<串GT;的azazaz =新的ArrayList<串GT;();                                    mapId.add(TAG_ID);                                    Log.e(出价,出价);                                    而(cursors.moveToNext())
                                    {
                                     通信System.err.println(更新codeD STEON+ cursors.getString(0));
                                     Log.e(投标,c.getString(TAG_ID));                                    }
                             }                         }
                             赶上(JSONException E){
                                 e.printStackTrace();
                             }
                    }
                    其他{
                        Log.i(完成好,M这里);
                    }
                    返回null;
            }

背后

原因

 如果(data_exist!= bookProduct.length()){

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

bookProduct.length 是我要对它们进行比较,如果长度不一样jsonarray的长度,然后我想更新新的值到sqlite的。可以任何一个建议我的办法来做到这一点。


解决方案

  

如果长度不一样的话,我想更新的新值成
  sqlite的。


不更新数据库的好方法。结果
假设有在服务器数据库中的字段的一些变化。同样计数,但内容是不同的。结果,
这是我做的方式:

 字符串SQL =SELECT * FROM+ DB_TABLE +,其中+ TAG_ID +=+身份证+';
 光标C = database.rawQuery(SQL,NULL);
 c.moveToFirst();
 如果(c.getCount()大于0){//已经在数据库中
      //与TAG_ID = ID更新条目
  }
  其他{//新条目
      //添加新条目
  }

I want to compare the value of the id in sqlite to the value of TAG_ID coming from the json webservice.I just stuck up to do this..

What I have tried so far is

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

The reason behind

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

this statement is the variable data_exist=cursor.getCount();

and the bookProduct.length is the length of the jsonarray I want to compare them if the length is not same then I want to update the new values into the sqlite.could any one suggest me the approach to do this

解决方案

if the length is not same then I want to update the new values into the sqlite.

Not a good way to update databases.
Suppose that there is some changes in the server database fields. Same count but the contents is different.
This is the way I do:

 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
  }

这篇关于如何SQLite表的值进行比较,以在Android上的JSON标记值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆