Reputation: 376
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.
Upvotes: 0
Views: 61
Reputation: 11028
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
...
Upvotes: 1