Sie sollten wahrscheinlich eine gespeicherte Prozedur verwenden, um dies zu tun:
DELIMITER $$
DROP PROCEDURE IF EXISTS `test`.`DeleteByID` $$
CREATE PROCEDURE `test`.`DeleteByID` (db VARCHAR(64),tb VARCHAR(64),id_to_delete INT)
BEGIN
DECLARE FoundCount INT;
SELECT COUNT(1) INTO FoundCount
FROM information_schema.tables
WHERE table_schema = db
AND table_name = tb;
IF FoundCount = 1 THEN
SET @sql = CONCAT('DELETE FROM ',db,'.',tb,' WHERE id=',id_to_delete);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END IF;
END $$
DELIMITER ;
Alles, was Sie in Ihrem Code tun, ist die gespeicherte Prozedur aufzurufen. So löschen Sie beispielsweise die ID 128 aus drupaldb.comments
:
CALL test.DeleteByID('drupaldb','comments',128);
Versuche es !!!
Eine andere Variante wäre, die Abfrage direkt zu formen:
set @given_db = 'drupaldb';
set @given_tb = 'comments';
set @given_id = 128;
set @good_sql = CONCAT('DELETE FROM ',@given_db,'.',@given_tb,' WHERE id=',@given_id);
set @evil_sql = 'SELECT 1';
SELECT IF(table_exists=1,@good_sql,@evil_sql) INTO @DeleteSQL
FROM
(
SELECT COUNT(1) table_exists
FROM information_schema.tables
WHERE table_schema=@given_db
AND table_name=@given_tb
) A;
PREPARE stmt FROM @DeleteSQL; EXECUTE stmt; DEALLOCATE PREPARE stmt;
HINWEIS: Wenn die Tabelle nicht vorhanden ist, wird die Abfrage ausgeführt SELECT 1
.
Hier ist ein Beispiel, das zeigt, ob die Tabelle nicht vorhanden ist:
mysql> set @given_db = 'drupaldb';
Query OK, 0 rows affected (0.00 sec)
mysql> set @given_tb = 'comments';
Query OK, 0 rows affected (0.00 sec)
mysql> set @given_id = 128;
Query OK, 0 rows affected (0.00 sec)
mysql> set @good_sql = CONCAT('DELETE FROM ',@given_db,'.',@given_tb,' WHERE id=',@given_id);
Query OK, 0 rows affected (0.00 sec)
mysql> set @evil_sql = 'SELECT 1';
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT IF(table_exists=1,@good_sql,@evil_sql) INTO @DeleteSQL
-> FROM
-> (
-> SELECT COUNT(1) table_exists
-> FROM information_schema.tables
-> WHERE table_schema=@given_db
-> AND table_name=@given_tb
-> ) A;
Query OK, 1 row affected (0.00 sec)
mysql> PREPARE stmt FROM @DeleteSQL; EXECUTE stmt; DEALLOCATE PREPARE stmt;
Query OK, 0 rows affected (0.00 sec)
Statement prepared
+---+
| 1 |
+---+
| 1 |
+---+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
mysql>
Hier ist ein Beispiel, in dem die Tabelle vorhanden ist:
mysql> select count(1) from mysql.user where user='rolando';
+----------+
| count(1) |
+----------+
| 0 |
+----------+
1 row in set (0.00 sec)
mysql> grant all on *.* to 'rolando'@'127.0.0.1';
Query OK, 0 rows affected (0.00 sec)
mysql> select count(1) from mysql.user where user='rolando';
+----------+
| count(1) |
+----------+
| 1 |
+----------+
1 row in set (0.00 sec)
mysql> set @given_db = 'mysql';
Query OK, 0 rows affected (0.00 sec)
mysql> set @given_tb = 'user';
Query OK, 0 rows affected (0.00 sec)
mysql> set @given_id = 'rolando';
Query OK, 0 rows affected (0.00 sec)
mysql> set @good_sql = CONCAT('DELETE FROM ',@given_db,'.',@given_tb,' WHERE user=''',@given_id,'''');
Query OK, 0 rows affected (0.00 sec)
mysql> set @evil_sql = 'SELECT 1';
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT IF(table_exists=1,@good_sql,@evil_sql) INTO @DeleteSQL
-> FROM
-> (
-> SELECT COUNT(1) table_exists
-> FROM information_schema.tables
-> WHERE table_schema=@given_db
-> AND table_name=@given_tb
-> ) A;
Query OK, 1 row affected (0.00 sec)
mysql> PREPARE stmt FROM @DeleteSQL; EXECUTE stmt; DEALLOCATE PREPARE stmt;
Query OK, 0 rows affected (0.02 sec)
Statement prepared
Query OK, 1 row affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
mysql> select count(1) from mysql.user where user='rolando';
+----------+
| count(1) |
+----------+
| 0 |
+----------+
1 row in set (0.00 sec)
mysql>