gpt4 book ai didi

java - 我无法使用 ormlite android 更新表

转载 作者:行者123 更新时间:2023-12-01 15:02:53 24 4
gpt4 key购买 nike

问题是我有一个表产品,但我的更新脚本显然无法工作。一切都是返回 false。

产品.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;

}

我可以创建和删除,但无法更新。我尝试了一切。如果您花点时间回答我的问题,我将不胜感激。

最佳答案

对于其他开发人员来说,如果您遇到这样的问题,您应该确保表必须有一个身份 key 。

@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;
}

关于java - 我无法使用 ormlite android 更新表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13385714/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com