建表和数据准备
在 blog 库下建表:
create table `user_info` (
`id` bigint unsigned not null auto_increment,
`name` varchar(45) not null default '',
primary key (`id`)
) engine = InnoDB default charset = utf8mb4;
插入数据,数据内容如下:
mysql> select * from user_info;
+----+---------+
| id | name |
+----+---------+
| 2 | name001 |
| 3 | name002 |
| 4 | name003 |
| 5 | name004 |
| 6 | name005 |
| 7 | name006 |
+----+---------+
6 rows in set (0.01 sec)
删除数据
import pymysql
conn = pymysql.connect(host='127.0.0.1',
user='root',
password='123456',
port=3306,
database='blog',
autocommit=True
)
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
affect_rows = cursor.execute('delete from user_info where id=%s', [2])
print('影响行数: ', affect_rows)
cursor.close()
conn.close()
运行结果:
影响行数: 1