Java 我能';t使用ormlite android更新表
问题是我有一个表产品,我的更新脚本不能正常工作。这一切都是假的 产品类别Java 我能';t使用ormlite android更新表,java,android,sqlite,updates,ormlite,Java,Android,Sqlite,Updates,Ormlite,问题是我有一个表产品,我的更新脚本不能正常工作。这一切都是假的 产品类别 @DatabaseTable(tableName = "Product") public class Product { @DatabaseField(index = true, generatedId = true) private int productId; @DatabaseField private String name; @DatabaseField private int quantity; //@
@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;
}
}
我可以创建和删除,但不能更新。我什么都试过了。
如果您能花点时间回答我的问题,我将不胜感激。对于其他开发人员,如果您遇到这样的问题,您应该确保表必须具有标识键
@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;
}
解决办法是
只需从数据库中获取对象产品的实例,然后修改以最终发送到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);
然后更新我的对象。注意对象Id(应相同),并使用natif函数更新(产品) 在您的情况下,必须重写equals和hashCode。为什么不使用DAO的方法
update(Product)
?1)是否引发异常(请参阅日志)?如果是,则显示堆栈跟踪。2) 更新是否返回1?3) dao是非空的吗?您当然应该使用dao.update(p)。您使用的是什么版本的ormlite?版本4.39在Android下返回更改行数方面做了一些修改。有没有可能在更新之前没有在数据库中创建对象?你试过dao.createOrUpdate(p)吗?@1。没有例外2。返回值为0 3。dao不是空的:(@Gray版本是4.42,我唯一要做的就是加载和编辑数据库中的记录。如果我使用dao.createOrUpdate函数,总是在数据库中生成一条新记录。这很奇怪
@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;
}
// 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);