在sqlite数据库android中保存和检索键值对 [英] saving and retrieving key-value pairs in an sqlite database android

查看:25
本文介绍了在sqlite数据库android中保存和检索键值对的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天,基本上我正在寻找一种方法来检索和保存与 sqlite 数据库中的列项目相关的键值对,以便我可以更新列表视图.

Good day, Basically i am looking for a way to retrieve and save a key-value pair with respect to a column item in sqlite database so that i can update a listview.

例如:如果我有一个名为group"的列,其中有一个名为family"的条目;关于该项目,我希望能够为该项目家庭"保存或检索联系人列表(姓名和号码).另一个项目条目相同.

Example: if i have a column named "group" with an item entry named "family"; with respect to that item, i would like to be able to save or retrieve a list of contacts (name and number) just for that item "Family". same for another item entry.

这样做的最佳方法是什么?我尝试通过 json 使用 Hashmap 的 ArrayList 将联系人详细信息保存在表格的一列中.但这样做有麻烦.我试过这个:

what would be the best way to do this?. i tried saving the contacts details in a column on the table, using an ArrayList of a Hashmap via json. but having troubles doing it this way. i tried this:

保存时:

//already have the name and number from using the android contacts api

    final ArrayList<HashMap<String,String>> contacts = new ArrayList<HashMap<String,String>>();     
    HashMap<String, String> map = new HashMap<String,String>();
            map.put(name, number);
            contacts.add(map);

            JSONObject json_contacts = new JSONObject();

                try {
            json_contacts.put("contacts_Arrays", new JSONArray(contacts));
                    arraylist_to_string = json_contacts.toString();

                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            ContentValues values = new ContentValues(); 
            values.put(ContactDB.GROUP, groupname);
            values.put(ContactDB.GROUP_CONTACTS, arraylist_to_string);

            Log.d(TAG, "Successfully updating contacts for group");
            dbadapter.updateGroup(rowId, values);
            cursoradapter.notifyDataSetChanged();

在我的 bindview 方法中检索时:

when retrieving in my bindview method:

String contactDetails = cursor.getString(cursor.getColumnIndex(ContactDB.GROUP_CONTACTS));

String contactDetails = cursor.getString(cursor.getColumnIndex(ContactDB.GROUP_CONTACTS));

        if(contactDetails != null){
            try {
                JSONObject json = new JSONObject(contactDetails);
                JSONArray json_array = json.optJSONArray("contacts_Arrays");

                for(int i=0; i < json_array.length(); i++){
    LinkedHashMap<String,String> map = new LinkedHashMap<String, String>();
            Iterator<String> myIterator = map.keySet().iterator();
                    while(myIterator.hasNext()){
                       key = (String)myIterator.next();
                       value = (String)map.get(key);

                       holder.contact_name.setText(key);
                       holder.contact_number.setText(value);                           
                       notifyDataSetChanged();

                    }
                }


            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }else{
            return;
        }
    }

根本没有任何运气,并且首先不确定它是否是正确的方法.构建数据库表的最佳方法是什么.我应该创建一个联系人对象并存储它还是有更好的方法?谢谢

Not having any luck with it at all and not sure if its the correct way to do that in the first place. what would the best way to structure the database table. should i create a contact object and store it or is there a better way? Thank you

推荐答案

仅通过查看您的代码,List 中的多个 HashMap 实例可能会造成内存过大,取决于您存储数据的大小.

Just by looking at your code, multiple HashMap instances in a List might be a memory overkill, depending on the size of your stored data.

由于您要存储键值对,因此通常不是正确的决定,关系数据库(如 SQLite 就是其中之一).Android 有 共享首选项" 来存储简单的键值对(具有原始数据类型).

Since you want to store key-value pairs, a relational database (like SQLite is one) is most often not the right decision. Android has the "Shared Preferences" to store simple key-value pairs (with primitive data-types).

如果你想/需要用 SQLite 实现它,你应该采用关系方式.如果我正确理解您的问题,您会得到如下表格:

If you want/need to implement it with SQLite, you should go the relational way. If I understood your question correctly, you would have tables like the following:

Table "Groups"
+----------+------------+
| group_ID | group_name |
+----------+------------+
| 1        | Family     |
| 2        | Work       |
+----------+------------+

Table "Contacts"
+----------+------------+--------+
| name     | number     | group  |
+----------+------------+--------+
| Jon      | 0173401... | 1      |
| James    | 057123...  | 2      |
| Lukas    | 012343...  | 2      |
+----------+------------+--------+

从此表中,您可以查询James"和卢卡斯"工作"组的成员.然后,您可以查询工作"的所有成员.通过简单地搜索将 group 列设置为 group 表中显示的值的条目(对于2"),进行分组.工作"-组).

From this table, you can query that "James" and "Lukas" are members of the "Work"-group. You could then query all members of the "Work" group by simply searching for entries with the group-column set to the value presented in the group-table (which is 2 for the "Work"-group).

借助此系统,您可以添加新的群组和联系人,并将联系人放入(或多个)群组中.

With this system, you can then add new groups and contacts and put contacts in on (or multiple) groups.

这篇关于在sqlite数据库android中保存和检索键值对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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