简体   繁体   中英

How to insert or update a particular field of database row?

I am using a database table with my android application. What i want to do is insert a row if does not exist else update an entry in it? I am using the following code but found no luck.

public boolean insertIntoOrder(String couponBrand, String couponTitle, String couponDenomination, int couponCount) {
        database = this.getWritableDatabase();
        ContentValues orderContentValues = new ContentValues();
        orderContentValues.put(KEY_ORDER_BRAND, couponBrand);
        orderContentValues.put(KEY_ORDER_TITLE, couponTitle);
        orderContentValues.put(KEY_ORDER_DENOMINATION, couponDenomination);
        orderContentValues.put(KEY_ORDER_COUPON_COUNT, couponCount);
        boolean responseValue = checkOrderConfirmation(couponBrand, couponTitle, couponDenomination);
        if (responseValue == true) {
            database.insert(TABLE_ORDER_CONFIRMATION, null, orderContentValues);
        }
        else {
            couponCount = couponCount++;
            database.execSQL("UPDATE " + TABLE_ORDER_CONFIRMATION + " SET " + KEY_ORDER_COUPON_COUNT + " = ? WHERE " + KEY_ORDER_BRAND + " = ? AND " + KEY_ORDER_TITLE + " = ? AND " + KEY_ORDER_DENOMINATION + " = ?", new String[] {couponCount + "", couponBrand, couponTitle, couponDenomination});
        }
        return true;
    }

public boolean checkOrder(String couponBrand, String couponTitle, String couponDenomination) {
        database = this.getReadableDatabase();
        try {
            checkOrderConfirmationCursor = database.rawQuery("SELECT * FROM " + TABLE_ORDER_CONFIRMATION + " WHERE " + KEY_ORDER_BRAND + " = ? AND " + KEY_ORDER_TITLE + " = ? AND " + KEY_ORDER_DENOMINATION + " = ?", new String[] {couponBrand, couponTitle, couponDenomination});

            if (checkOrderConfirmationCursor == null) {
                if (checkOrderConfirmationCursor.getCount() <= 0) {
                }
            }
            else {
                return true;
            }
        }
        catch (SQLiteException sqle) {
            sqle.printStackTrace();
        }
        return false;
    }

I want to update coupon count if the new row contains duplicate data but doesn't get something to do so. Any help would be appreciable. Thanks in advance.

Try changing checkOrder to:

public boolean checkOrder(String couponBrand, String couponTitle, String couponDenomination) {
    database = this.getReadableDatabase();
    try {
        Cursor checkOrderConfirmationCursor = database.rawQuery("SELECT * FROM " + TABLE_ORDER_CONFIRMATION + " WHERE " + KEY_ORDER_BRAND + " = ? AND " + KEY_ORDER_TITLE + " = ? AND " + KEY_ORDER_DENOMINATION + " = ?", new String[] {couponBrand, couponTitle, couponDenomination});

        if (checkOrderConfirmationCursor != null
                && checkOrderConfirmationCursor.getCount() > 0) {
            return true
        }
    }
    catch (SQLiteException sqle) {
        sqle.printStackTrace();
    }
    return false;
}

Also your main method is calling checkOrderConfirmation but the method is named checkOrder ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM