【问题标题】:Inserted rows are not actually inserted插入的行实际上并未插入
【发布时间】:2016-02-17 04:37:04
【问题描述】:

我正在通过网络以 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;
}

在主要活动中:

boolean add = new TableControllerTenant_pg(getApplicationContext()).create(listPG);

【问题讨论】:

  • 所以你说你的插入工作但你的显示没有,然后你发布了工作的代码 - 我很困惑。
  • 检查您的数据库文件以确保数据已插入数据库中

标签: java android json sqlite


【解决方案1】:

您正在呼叫beginTransaction()。 它的文档说:

如果任何事务在未标记为干净的情况下结束(通过调用setTransactionSuccessful),更改将被回滚。

【讨论】:

  • 非常感谢。我多么白痴,不包括在内!我差点忘了。谢谢!
猜你喜欢
  • 2015-08-17
  • 1970-01-01
  • 2012-11-22
  • 1970-01-01
  • 2011-02-16
  • 2022-11-20
  • 2014-08-18
  • 2022-01-23
  • 1970-01-01
相关资源
最近更新 更多