繁体   English   中英

如何将数组插入SQLite?

[英]How to insert an array to SQLite?

我需要将一个由JSON加载的数组插入到SQLite数据库中。 任何帮助表示赞赏。

JSONObject jsonObj = new JSONObject(jsonStr);


                    JSONArray sites = jsonObj.getJSONArray("Sites");

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

                        String id = c.getString("id");
                        String nam = c.getString("names");
                        String loc = c.getString("location");

                        HashMap<String, String> sit = new HashMap<>();

                        sit.put("id", id);
                        sit.put("names", name);
                        sit.put("location", loc);

                        array_sites.add(sit);

尝试这个:

public void addData(ArrayList<String> list){

    int size = list.size();

    SQLiteDatabase db = getWritableDatabase();

    try{
       for (int i = 0; i < size ; i++){
           ContentValues cv = new ContentValues();
           cv.put(KEY_NAME, list.get(i));
           Log.d("Data Inserted ",""+ cv);
           db.insertOrThrow(TABLE_NAME, null, cv);
       }  
       db.close();
    }catch (Exception e){
    Log.e("Exception>>>>", e + " ");
}

从您存储数据的地方拨打电话

addData(array_sites);

我建议使用ArrayList<String> ,你可以使用自己的ArrayList<YourDataType>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM