我正在尝试将多个数据插入到sqlite中,我以json格式从服务器获取数据,我从我的活动中解析数据。并尝试保存到我的数据库中。但我想知道,我如何多次在sqlite中保存数据? 我得到一些参考资料,并检查我的logcat一切都没问题。我怎么解决这个问题?提前致谢
public List<ModelAgen> updateTaccessUser(){
List<ModelAgen> data = new ArrayList<ModelAgen>();
SQLiteDatabase db = this.getWritableDatabase();
db.beginTransaction();
try {
String update = "INSERT INTO taccesagen(" +
"id," +
"ref_magen," +
"ref_tuser," +
"def," +
"nama" +
")" +
" VALUES" +
"(?, ?, ?, ?, ?)";
for (int i = 0; i < data.size(); i++) {
String kondisi;
if (data.get(i).getDef() == "t") {
kondisi = "1";
} else {
kondisi = "0";
}
SQLiteStatement insert = db.compileStatement(update);
insert.bindLong(1, data.get(i).getId());
insert.bindString(2, data.get(i).getRef_magen());
insert.bindString(3, data.get(i).getRef_tuser());
insert.bindString(4, kondisi);
insert.bindString(5, data.get(i).getNama());
insert.execute();
}
db.setTransactionSuccessful();
}finally {
db.endTransaction();
}
return data;
}
答案 0 :(得分:2)
您可以像这样插入多条记录
try {
for(int i=0;i<lstModelAgen.size();i++) {
ContentValues contentValues = new ContentValues();
contentValues.put("column1", lstModelAgen.get(i).getcolumn1Value);
contentValues.put("column2", lstModelAgen.get(i).getcolumn2Value());
db.insert("taccesagen", null, contentValues);
}
} catch (Exception e) {
e.printStackTrace();
}
答案 1 :(得分:1)
尝试以下
for (int i = 0; i < data.size(); i++) {
String kondisi;
if (data.get(i).getDef() == "t") {
kondisi = "1";
} else {
kondisi = "0";
}
ArrayList<ContentValues> arrvals = new ArrayList<ContentValues>();
ContentValues values = new ContentValues();
values.put("id", data.get(i).getId());
values.put("ref_magen", data.get(i).getRef_magen());
values.put("ref_tuser",data.get(i).getRef_tuser() );
values.put("def", kondisi);
values.put("nama", data.get(i).getNama());
arrvals.add(values);
}
db.InsertData("taccesagen", arrvals);