How to save my Arraylist into SQLite database?

岁酱吖の 提交于 2019-12-29 09:50:33

问题


I want to save my ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>() in SQLite database and later retrieve it to display the items stored in it. How can I achieve this? Please provide examples. Thanks in Advance :)


回答1:


Tutorial Link

Please refer to the link above. I has a working example of using HashMap for SqLite operations using SQliteOpenHelper. It has one insertion at a time, but you might wanna create a loop, if you wish to insert them all in one instance.




回答2:


You can serialize it using the default standard java serializer or us your own lib. Following your perf. needs: you can read this benchmark: https://github.com/eishay/jvm-serializers/wiki




回答3:


Is there more than one records in your HashMap. If so, You will save every records. There are best way save to file as a serialized object Save to file:

FileOutputStream fos = null;
    ObjectOutputStream oos = null;
    try {
        List<Map<String, String> list=new ArrayList<HashMap<String, String>>();

        fos = getContext().openFileOutput(OFFERS_FILE_NAME,
                Context.MODE_PRIVATE);
        oos = new ObjectOutputStream(fos);
        oos.writeObject(list);

    } catch (JSONException e) {
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (fos != null) {
                fos.flush();
                fos.close();
            }
            if (oos != null) {
                oos.flush();
                oos.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Read from file:

List<Map<Integer, String>> orderData = null;

FileInputStream fis = null;
ObjectInputStream ois = null;
try {
fis = getContext().openFileInput(OFFERS_FILE_NAME);
ois = new ObjectInputStream(fis);
orderData = (HashMap<Integer, String>) ois.readObject();
} catch (Exception e) {
e.printStackTrace(); // We have not data in SD Card

} finally {
        try {
        if (fis != null)
            fis.close();
        if (ois != null)
            ois.close();
        } catch (Exception e) {
}
        }



回答4:


for (int i = 0; i < yourlistname.size(); i++) 
                    {
                            String sql = "INSERT or replace INTO tbl_Resources (resourceID, contentID, imgpath) "

                                            + "VALUES ('"
                                            + sitesList.get(i)
                                            + "', '"
                                            + sitesList.get(i)
                                            + "', '"
//                                          
                                            +sitesList.get(i)
                                            + "')";                 
                        db.execSQL(sql);

                    }   


for (int i = 0; i < list.size(); i++) {

            //insert stat like by this you will get listCountry.get(i))

        }



回答5:


How about you itterate through that arraylist and just add it to the database...?



来源:https://stackoverflow.com/questions/22496594/how-to-save-my-arraylist-into-sqlite-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!