Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/209.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在删除受影响的行之前识别它们_Android_Android Sqlite - Fatal编程技术网

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
}