【问题标题】:inserting arraylist<String> values to sqlite in android在android中将arraylist <String>值插入sqlite
【发布时间】:2016-04-14 23:56:06
【问题描述】:

我在 2 个不同的 arraylist 中有来自 listview 的名称和 id,并且想将 id 和名称从 arraylist 存储到字符串,然后将它们保存在单击到 sqlite 的按钮上。

以下代码的问题是,当检查2个项目并单击按钮时,物品已插入两次。当 id 设置为 UNIQUE 时,插入 3 个 id 时,只有一个保存在 sqlite 中,...

这是我迄今为止尝试过的:

 public void addSelected(ArrayList<String> selNames, ArrayList<String> selID) {

    SQLiteDatabase db = getWritableDatabase();
    try{

        for (String str: selID){
            for (String stl:selNames){
            ContentValues cv = new ContentValues();
            cv.put(KEY_ID, str);
            cv.put(KEY_STATUS, stl);
            db.insertOrThrow(TABLE_SELECTED, null, cv);

            }
        }
        db.close();
    }catch (Exception e){
        Log.e("Problem", e + " ");
    }


}

【问题讨论】:

    标签: android sqlite for-loop arraylist android-sqlite


    【解决方案1】:

    如果你的两个数组大小相同,我认为你应该简单地尝试一下......并且像这样在 db.insertorThrow 中传递你的简历

    public void addSelected(ArrayList<String> selList, ArrayList<String> selID){
    
            int size = selID.size();
    
            SQLiteDatabase db = getWritableDatabase();
           try{
           for (int i = 0; i < size ; i++){
                ContentValues cv = new ContentValues();
               cv.put(KEY_ID, selID.get(i));
                 cv.put(KEY_STATUS, selList.get(i));
                Log.d("Added ",""+ cv);
                db.insertOrThrow(TABLE_SELECTED, null, cv);
            }  
    db.close();
        }catch (Exception e){
            Log.e("Problem", e + " ");
        }
    

    【讨论】:

      【解决方案2】:

      如果你想在不迭代的情况下放置数组列表,你可以执行以下方法。

      /此代码可用于插入/

      JSONObject json = new JSONObject();// Create one Jsonobject
      json.put("uniqueArrays", new JSONArray(items));// put the ArrayList You have in hand by converting that to JSONArray.
      String arrayList = json.toString();//Convert json to string
      

      /这段代码可以用来获取数据/

       JSONObject json = new JSONObject(stringreadfromsqlitetable);// Convert the String to JsonObject
        ArrayList items = json.optJSONArray("uniqueArrays");// Conver the jsonObject to Arraylist
      

      【讨论】:

        【解决方案3】:

        您正在迭代 ID,然后为每个 ID 迭代所有名称,从而插入两者的所有组合。相反,您可以迭代两个列表的 length

        public void addSelected(ArrayList<String> selNames, ArrayList<String> selID) {
        
            int size = selNames.size();
        
            // Check that both lists have the same size
            if (size != selID.size()) {
                throw new IllegalArgumentException();
                // Or some more elegant way to handle this error condition
            }
        
            SQLiteDatabase db = getWritableDatabase();
            try{
                for (int i = 0; i < size; ++i) {
                    ContentValues cv = new ContentValues();
                    cv.put(KEY_ID, selID.get(i));
                    cv.put(KEY_STATUS, selNames.get(i));
                    db.insertOrThrow(TABLE_SELECTED, null, cv);
                }
                db.close();
            } catch (Exception e){
                Log.e("Problem", e + " ");
            }
        }
        

        注意:对db.close() 的调用可能应该在finally 块中,而不是try 块中。我把它留在那里,就像它在 OP 中一样,因为这不是问题的问题,但你可能也应该在你的真实代码中处理它。

        【讨论】:

        • @murtazaali 愿意分享一些更具体的细节吗?
        • 我将与您分享 logcat 错误消息,请检查
        • 这在 logcat 01-11 14:00:04.807 6183-6183/ali.murtaza.jsontest E/SQLiteLog 中显示:(1) 在“null”附近:语法错误 01-11 14:00 :04.807 6183-6183/ali.murtaza.jsontest E/问题:android.database.sqlite.SQLiteException:接近“null”:语法错误(代码1):,编译时:INSERT INTO Selected_table(null)值(NULL)
        猜你喜欢
        • 2014-02-27
        • 2011-09-09
        • 2018-01-05
        • 2012-04-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多