Android 在删除受影响的行之前识别它们
我想知道是否有可能在实际运行删除之前检查受删除影响的行数:Android 在删除受影响的行之前识别它们,android,android-sqlite,Android,Android Sqlite,我想知道是否有可能在实际运行删除之前检查受删除影响的行数: public static int deletePremixable(Context context, Premixable premixable) { Long id = premixable.getId(); SQLiteDatabase db = getWritableDatabase(context); String selection = PremixableEntry._
public static int deletePremixable(Context context, Premixable premixable) {
Long id = premixable.getId();
SQLiteDatabase db = getWritableDatabase(context);
String selection = PremixableEntry._ID + "=?";
String[] selectionArgs = new String[]{String.valueOf(id)};
// Here I would like to do something like
// if(db.deleteAffectedRows((PremixableEntry.TABLE_NAME, selection, selectionArgs) > 1) {
// throw Exception('It should never be more than one line deleted here!');
// }
return db.delete(PremixableEntry.TABLE_NAME, selection, selectionArgs);
}
您可以使用事务,例如
db.beginTransaction();
try {
// delete and check the result
// you can use several queries
if (everything_ok) {
db.setTransactionSuccessful();
}
} finally {
// if the transaction was not set successful
// all changes since beginTransaction will be reverted
db.endTransaction();
}
您可以使用事务,例如
db.beginTransaction();
try {
// delete and check the result
// you can use several queries
if (everything_ok) {
db.setTransactionSuccessful();
}
} finally {
// if the transaction was not set successful
// all changes since beginTransaction will be reverted
db.endTransaction();
}
您可以检查有多少行会受到影响
Cursor res = db.rawQuery("SELECT * FROM " + PremixableEntry.TABLE_NAME + " WHERE " + selection, selectionArgs);
if (res.getCount() == 1){
//do the delete
}
您可以检查有多少行会受到影响
Cursor res = db.rawQuery("SELECT * FROM " + PremixableEntry.TABLE_NAME + " WHERE " + selection, selectionArgs);
if (res.getCount() == 1){
//do the delete
}