2013-05-28 109 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(); 
}