user1209216
user1209216

Reputation: 7954

Android/Ormlite - update existing or insert if not exists

I have Shops table, corresponding fields for ormlite are declared as follows:

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

Next, this is method for update existing row (identified by primary key, right?):

 public void updateShop(Shops item)
    {
        try
        {
            getHelper().getShopItemsDao().update(item);
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
    }

Now I need to change my updateShop method into updateOrInsertShop. So if specified shop exists (indetified by shopID) it should be updated. Otherwise, it should be inserted as new row. Obviously, I can perform select by shopID to determine if certain shop already exists and then perform update (as above) or create if not exists.

Is there any better way?

Upvotes: 0

Views: 1473

Answers (1)

Punit Sharma
Punit Sharma

Reputation: 2947

you can use following method

 createOrUpdate(parameter) 

Upvotes: 1

Related Questions