2013-05-28 112 views
0

我需要在我的數據庫SQLite中插入多條記錄。如何在一個SQLite查詢中插入多個記錄?

我得到一個ArrayList的對象類型爲「Nota」,我需要完全插入這個ArrayList。

我的對象「Nota」有方法getId和getTitle。所以我可以做arrayList_nota.get(Attribute)來獲取所有的屬性並插入。

我的數據庫SQLite有: ID |標題|日期|文字|

但我不知道是否有可能。我不知道這個查詢sintax。我需要插入數組的每個位置和她的屬性。

如何在一個SQLite查詢中插入多個記錄?

回答

0
Insert into Nota (ID,Title,Date,Text) 
Select '1','LOTR 1','2010-01-01','Lord of The Rings 1' 
union Select '2','LOTR 2','2010-02-01','Lord of The Rings 2' 
union Select '3','Lotr 3','2010-03-01', 'Lord of the Rings 3';` 
0

這不是一個sql語句解決方案。

try { 
    // db is your SQLiteDatabase object. 
    db.beginTransaction(); 
    for (Nota n : arrayList_nota) { 
     ContentValues values = new ContentValues(); 
     values.put(ID, n.getId()); 
     values.put(TITLE, n.getTitle()); 
     values.put(DATE, n.getDate()); 
     values.put(TEXT, n.getText()); 
     db.insert(TABLE_NAME, null, values); 
    } 
    db.setTransactionSuccessful(); 
} catch (SQLiteException e) { 
    // handle your sqlite database exceptions. 
} finally { 
    db.endTransaction(); 
}