How to save my Arraylist into SQLite database?

后端 未结 5 2247
独厮守ぢ
独厮守ぢ 2020-12-22 06:31

I want to save my ArrayList> mylist = new ArrayList>() in SQLite database and later retr

相关标签:
5条回答
  • 2020-12-22 07:21

    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

    0 讨论(0)
  • 2020-12-22 07:22

    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.

    0 讨论(0)
  • 2020-12-22 07:23

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

    0 讨论(0)
  • 2020-12-22 07:34
    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))
    
            }
    
    0 讨论(0)
  • 2020-12-22 07:37

    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) {
    }
            }
    
    0 讨论(0)
提交回复
热议问题