2017-03-06 76 views
3

我有一種方法可以將值插入到類sqlite中。而在主要活動中,我正在解析json值並嘗試將值插入到循環中的sqlite中,因爲我必須插入多行。但只有一行被添加到數據庫中。如何在sqlite中插入多行android

方法來添加多個行是如下:

public void addSession(String sessionname,String start_time,String end_time,String id) { 
     SQLiteDatabase db = this.getWritableDatabase(); 
     try { 
      db.beginTransaction(); 
      String sql = "Insert into session (id, sessionname,start_time, end_time) values(?,?,?,?)"; 
      SQLiteStatement insert = db.compileStatement(sql); 
      insert.bindString(1, id); 
      insert.bindString(2, sessionname); 
      insert.bindString(3, start_time); 
      insert.bindString(4, end_time); 
      insert.execute(); 
      db.setTransactionSuccessful(); 
     } catch (Exception e) { 
      Log.w("Appet8:",e); 
     } finally { 
     db.endTransaction(); 
     } 
    } 

在下面的for循環中我試圖將值添加到SQLite表:

JSONObject jsonObject = new JSONObject(response); 
JSONArray foodsessions = jsonObject.getJSONArray("foodsessions"); 
for(int i=0;i<foodsessions.length();i++) { 
        JSONObject session_object = foodsessions.getJSONObject(i); 
        String session = session_object.getString("sessionname"); 
        String start_time = session_object.getString("start_time"); 
        String end_time = session_object.getString("end_time"); 
        String session_id = session_object.getString("id");         
db.addSession(session,start_time,end_time,session_id); 
         }      
+0

查詢這個線程可以幫助你:https://stackoverflow.com/q/1609637/468724 –

回答

3

更好的結構將在方法外創建一個db引用,並將其作爲參考傳遞給它:

低於

============== 請檢查用於插入多行:

// adb is SQLiteOpenHelper 
JSONObject jsonObject = new JSONObject(response); 
JSONArray foodsessions = jsonObject.getJSONArray("foodsessions"); 
int length = foodsessions.length(); 

for (int i = 0; i < length; i++) { 
    JSONObject o = foodsessions.getJSONObject(i); 
    String session = session_object.getString("sessionname"); 
    String start_time = session_object.getString("start_time"); 
    String end_time = session_object.getString("end_time"); 
    String session_id = session_object.getString("id");  

    SQLiteDatabase db = adb.getWritableDatabase(); 

    ContentValues newValues = new ContentValues(); 
    newValues.put(adb.ATTRIBUTE_session, session); 
    newValues.put(adb.ATTRIBUTE_start_time, start_time); 
    newValues.put(adb.ATTRIBUTE_end_time, end_time); 
    newValues.put(adb.ATTRIBUTE_session_id, session_id); 

    long res = db.insertWithOnConflict(adb.TABLE_NAME, null, newValues, SQLiteDatabase.CONFLICT_REPLACE); 
    db.close(); 
} 

更好的解決方案是避免使用原始查詢,如果它可以通過SQLiteDatabase提供的方法完成。

+0

無法在類之外獲得'db.beginTransaction();'。 For循環是在其他類 –

+0

請檢查我的更新,它應該沒關係,如果你使用ContentValues – Alan

+0

謝謝。您的解決方案奏效:) –

2

試試這個,

public void addSession(String session,String start_time,String end_time,String session_id) 
{ 
    SQLiteDatabase db = this.getWritableDatabase(); 
    ContentValues values = new ContentValues(); 
    values.put("sessionname", session); 
    values.put("start_time", start_time); 
    values.put("end_time",end_time); 
    values.put("id",session_id); 

    // insert row 
    db.insert(TABLE_DATA, null, values); 
} 
+0

從這我能夠只插入一行 –

0

這是簡單的:

INSERT INTO tb_units (sort, name, isdef) VALUES 
(1, "عدد",1), 
(2, "کیلوگرم",1), 
(3, "گرم",1), 
(4, "کیلومتر",1), 
(5, "متر",1), 
(6, "متراژ",1), 
(7, "مگابایت",1), 
(8, "کیلوبایت",1), 
(9, "مثقال",1), 
(10, "شاخه",1), 
(11, "صفحه",1), 
(12, "رول",1), 
(13, "بسته",1), 
(14, "کارتن",1), 
(15, "دستگاه",1), 
(16, "پاکت",1), 
(17, "مورد",1), 
(18, "جعبه",1), 
(19, "تن",1), 
(20, "قطعه",1), 
(21, "حلقه",1), 
(22, "تخته",1), 
(23, "جلد",1), 
(24, "لیتر",1), 
(25, "مترمکعب",1), 
(26, "مترمربع",1), 
(27, "دست",1), 
(28, "جین",1), 
(29, "اشتراک",1), 
(30, "سری",1), 
(31, "گیگابایت",1), 
(32, "جلسه",1), 
(33, "جفت",1), 
(34, "قراض",1), 
(35, "ساعت",1), 
(36, "تن",1); 

和我們db.execSQL