我对表商店的列定义:
@DatabaseField(canBeNull = false, columnName = C_SHOP_ID, index=true, id=true)
private String shopId;
@DatabaseField(dataType = DataType.BYTE_ARRAY, canBeNull = false, columnName = C_SHOP_ICON)
private byte[] shopIcon;
@DatabaseField(canBeNull = false, columnName = C_SHOP_DETAILS)
private String shopDetails;
@DatabaseField(canBeNull = false, columnName = C_SHOP_NAME)
private String shopName;
@DatabaseField(canBeNull = false, columnName = C_SHOP_SHORT_DESCRIPTION)
private String shopShorDescription;
接下来,执行更新(或插入)新行:
public void addOrUpdateShop(Shops c)
{
try
{
//getHelper().getShopItemsDao().create(c);
getHelper().getShopItemsDao().createIfNotExists(c);
}
catch (SQLException e)
{
e.printStackTrace();
}
}
有时,执行更新时不需要更改所有字段。例如,shopDetails或shopIcon(大blob图像)不应更新并保持原样。这由特定字段值指示。例如,如果不应在数据库中更新c.shopDetails
,则其值为"no change"
现在我需要创建更新行,但每当c.ShopDetails等于"no change"
时,我的表中的shopDetails字段不应该更新,因此表中的字段值不会被更改。其他字段应该更新。
如果没有ormlite,我需要编写所有相应的更新查询以从update语句中排除某些列,因为它是必需的。是否有任何简单的方法可以使用ormlite(不编写原始sql查询)?
此外,如果不存在,我需要创建新行(因此,更新构建器对此不太好,因为我只能用它更新现有字段)
答案 0 :(得分:0)
想象一下,您需要更新商店ID为25的商店对象的shopeName。
然后首先需要检索商店ID为25的Shop对象 然后,如果有这样的对象,想象一下将Shpops对象检索为oldShopObject。
现在您创建名为newShopObject的新店铺对象,如下所示
Shops newShopObject = new Shpos();
newShopObject.shopId = oldShopObject.shopId;
newShopObject.shopIcon = oldShopObject.shopIcon;
newShopObject.shopDetails = oldShopObject.shopDetails;
newShopObject.shopeName = <set your new shop name here>;
newShopObject.shopShortDescription = oldShopObject.shopShortDescription;
现在您可以保存此newShopObject。 按如下所示更新您的代码并尝试
public void addOrUpdateShop(Shops newShopObject)
{
try
{
getHelper().getShopItemsDao().createOrUpdate(newShopObject);
}
catch (SQLException e)
{
e.printStackTrace();
}
}
阅读here