我创建了一个测试表:
mysql> select version();
+-----------+
| version() |
+-----------+
| 8.2.0 |
+-----------+
mysql> create table workers (id serial primary key);
Query OK, 0 rows affected (0.01 sec)
mysql> insert into workers values (1), (2);
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from workers;
+----+
| id |
+----+
| 1 |
| 2 |
+----+
DEMO脚本DELETE-Test.py:
import mysql.connector
if __name__ == '__main__':
config = { ... }
mydb = mysql.connector.connect(**config)
mycursor = mydb.cursor()
# FIXED: use %s instead of %d
query = "DELETE FROM `workers` WHERE id = %s"
mycursor.execute(query, (1,))
# FIXED: mydb instead of mysql
mydb.commit()
mycursor.close()
mydb.close()
运行脚本:
% python3 --version
Python 3.11.4
% python3 ./delete-test.py
%
判断结果:
mysql> select * from workers;
+----+
| id |
+----+
| 2 |
+----+