【问题标题】:I Can't Update table with ormlite android我无法用 ormlite android 更新表
【发布时间】:2012-11-14 19:16:25
【问题描述】:

问题是我有一个表格产品,而我的更新脚本无法正常工作。全部返回 false。

Product.class

@DatabaseTable(tableName = "Product")
public class Product  {
@DatabaseField(index = true, generatedId = true)
private int productId;

@DatabaseField
private String name;

@DatabaseField
private int quantity;

//@DatabaseField(canBeNull = true)
//private Integer categorie;
//http://logic-explained.blogspot.com.ar/2011/12/using-ormlite-in-android-projects.html

@DatabaseField
private int categorie;

//@ForeignCollectionField
//private ForeignCollection<Categorie> itemsCategorie;  

@DatabaseField
private String description;

@DatabaseField
private String photo;

Product() {
}
public Product(int productId, String name, int quantity,            int categorie, String description, String photo) {
    super();
    this.productId = productId;
    this.name = name;
    this.quantity = quantity;
    this.categorie = categorie;
    this.description = description;
    this.photo = photo;
}



public void setDescription(String description) {
    this.description = description;
}
public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getAddress() {
    return description;
}

public void setAddress(String address) {
    this.description = address;
}
public int getProductId() {
    return productId;
}

public void setProductId(int productId) {
    this.productId = productId;
}

public int getQuantity() {
    return quantity;
}

public void setQuantity(int quantity) {
    this.quantity = quantity;
}

public int getCategorie() {
    return categorie;
}

public void setCategorie(int categorie) {
    this.categorie = categorie;
}

public String getPhoto() {
    return photo;
}

public void setPhoto(String photo) {
    this.photo = photo;
}
public CharSequence getDesc() {
    return null;
}


}

我的脚本更新产品

public boolean updateProduct(Product p) {
    boolean ret = false;
    if (productDao != null) {
        try {
            productDao = getDao(Product.class);

            UpdateBuilder<Product, Integer> updateBuilder = productDao
                    .updateBuilder();
            updateBuilder.updateColumnValue("name", p.getName());
            updateBuilder.updateColumnValue("quantity", p.getQuantity());
            updateBuilder.updateColumnValue("categorie", p.getCategorie());
            updateBuilder.updateColumnValue("description", p.getDesc());
            updateBuilder.updateColumnValue("photo", p.getPhoto());

            // but only update the rows where the description is some value
            updateBuilder.where().eq("productId", p.getProductId());
            // actually perform the update

            String str = updateBuilder.prepareStatementString();
            // UPDATE `Product` SET `name` = 'gcd' ,`quantity` = 1
            // ,`categorie` = 1 ,`description` = ? ,`photo` = '' WHERE
            // `productId` = 0

            if (productDao.update(updateBuilder.prepare()) != 1) {
                ret = false;
            } else {
                productDao.refresh(p);
                ret = true;
            }
        } catch (Exception e) {
            ret = false;
            e.printStackTrace();
        }
    }
    return ret;
}

然后我用这样的函数调用它,但总是返回 false :(

public boolean updateProduct(Product p) {
    boolean ret = false; 
    try {
        ret = getHelper().updateProduct(p);




    } catch (Exception e) {
        e.printStackTrace();
        ret =false;
    }
    return ret;

}

我可以创建和删除,但我不能更新。我什么都试过了。 如果您能花点时间回答我的问题,我将不胜感激。

【问题讨论】:

  • 为什么不用DAO的update(Product)方法呢?
  • 1) 是否引发异常(参见日志)?如果是,请显示堆栈跟踪。 2)更新是否返回 1 ? 3) dao 是非空的吗?
  • 当然应该使用 dao.update(p)。您使用的是什么版本的 ormlite? 4.39 版对返回 Android 下更改的行数进行了一些修复。有没有可能在您更新之前 没有在数据库中创建对象?你试过 dao.createOrUpdate(p) 吗?
  • @yDelouis 1.没有异常 2.返回值为0 3.dao不为null :(
  • @Gray 版本是 4.42 。我唯一要做的就是加载和编辑数据库中的记录。如果我使用 dao.createOrUpdate 函数总是在数据库中生成一条新记录。这很奇怪

标签: java android sqlite updates ormlite


【解决方案1】:

对于其他开发人员,如果您遇到这样的问题,您应该确保表必须具有身份密钥。

@DatabaseTable(tableName = "User")
public class User  {
    @DatabaseField(generatedId = true)
    public int id;
    @DatabaseField
    public String ServiceUserId;
    @DatabaseField
    public boolean IsActive;
    @DatabaseField
    public String FirstName;
    @DatabaseField
    public String LastName;
    @DatabaseField
    public String Key;
    @DatabaseField
    public String Email;
}

【讨论】:

    【解决方案2】:

    解决方案是 只需从数据库中获取对象 Product 的实例,然后修改以最终发送到 updateProduct 方法。

    例如,首先我需要先创建任何方法以通过 ID 获取对象

       // get the Instance calling to the  getProductByID
       Product p = getHelper().getProductByID(p.getId())
    
       //modify any value of the product
       p.setFirstName("asd");
    
       //call the update
       ret = getHelper().updateProduct(p);
    

    那么我的对象就更新了。

    【讨论】:

      【解决方案3】:

      注意对象Id(应该相同),使用natif函数update(Product);

      【讨论】:

        【解决方案4】:

        在您的情况下,您必须覆盖 equals 和 hashCode。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-27
          相关资源
          最近更新 更多