我正在使用我的android应用程序的数据库表。我想要做的是插入一行,如果不存在,否则更新其中的条目?我使用以下代码,但没有找到运气。
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;
}
我想更新优惠券数量,如果新行包含重复数据但是没有这样做的话。任何帮助都会很明显。提前谢谢。
答案 0 :(得分:1)
尝试将checkOrder
更改为:
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;
}
此外,您的主要方法是调用checkOrderConfirmation
,但该方法名为checkOrder
...