mysqlclient库
文档 https://mysqlclient.readthedocs.io
安装 pip install mysqlclient==1.3.12
安装报错参考网址:https://www.cnblogs.com/taiyangfeng/p/12562561.html
获取数据记录 fetchone, fetchmany, fetchall
代码:
import MySQLdb
conn = MySQLdb.connect(
host='192.168.1.10', # 主机
user='root', # 数据库用户名
passwd='root', # 数据库密码
db='course', # 数据库名
charset='utf8' # 编码
)
c = conn.cursor()
# 执行sql语句,查询
# c.execute('select * from sq_course')
# 执行sql语句,插入数据
c.execute("INSERT INTO sq_course(NAME,'desc',display_idx) VALUES('haha','描述',4)")
# 执行
conn.commit()
c.fetchone() # 获取一条数据
c.fetchall() # 获取全部数据
c.fetchmany()
# 关闭
conn.close()