pythonの连接MySQL数据库

1.要确保开发环境中安装了pymsql,如果没有安装那么在控制台输入:

pip3 install pymysql

安装完成后,打开编辑器:

#!/usr/bin/env python
import pymysql

# 创建连接
conn = pymysql.connect(host="127.0.0.1",port=3306,user="root",passwd="root",db="testuser")

# 创建游标
cursor = conn.cursor()

# 执行SQL 并返回受影响行数
#effect_row = cursor.execute("update tb_base_user set name='Lucy' where id = 6")

# 执行SQL,并返回受影响行数 executemany默认开启事务。
effect_row = cursor.executemany("insert into tb_base_user(name)values(%s)", [("James"),("DW")])

# 提交 采用了executemany 就必须要用提交,因为它默认开启事务
conn.commit()

# 关闭游标
cursor.close()

# 关闭连接
conn.close()

猜你喜欢

转载自www.cnblogs.com/pengpengzhang/p/9669111.html