我正在通过网络以 JSON 格式下载一些数据,检索后将其存储在 SqlLiteDatabase 中。早些时候,我用来获取数据并将其显示在 RecyclerView 中,但现在我将其存储在 SqlLite 数据库中,然后在 RecyclerView 中显示。然而,数据成功存储,因为它返回 true,但当我显示数据时,它什么也不显示。为了检查添加后的数据是否存在,我获取了可用数据的计数并返回 0。
代码如下:
public boolean create(List<PGCardItemReturn> objectPg) {
SQLiteDatabase db = this.getWritableDatabase();
PGCardItemReturn lastItem = null;;
db.beginTransaction();
String sql = "Insert or Replace into tenant_pg (id,furnishing,area,price,numberofbeds,image_url) values(?,?,?,?,?,?)";
SQLiteStatement insert = db.compileStatement(sql);
for(int i=0;i<objectPg.size();i++){
PGCardItemReturn item = objectPg.get(i);
if(i == (this.count()-1)){
lastItem = item;
}
insert.bindString(1, item.getId());
insert.bindString(2, item.getFurnishing());
insert.bindString(3, item.getArea());
insert.bindString(4, item.getPrice());
insert.bindString(5, item.getNumberofbeds());
insert.bindString(6, item.getImageUrl());
insert.execute();
}
db.endTransaction();
return true;
}
在主要 Activity 中:
boolean add = new TableControllerTenant_pg(getApplicationContext()).create(listPG);
最佳答案
您正在调用beginTransaction() 。 它的文档说:
The changes will be rolled back if any transaction is ended without being marked as clean (by calling setTransactionSuccessful).
关于java - 插入的行实际上并未插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35448044/