1. 介绍
在搭建测试框架过程中,会遇到需要频繁操作数据库的情况,会用到pymysql进行数据库的操作
PyMySQL 是一个纯 Python 实现的 MySQL 客户端库,支持兼容 Python 3
2.安装
pip install pymysql
3.使用步骤
3.1 与数据库建立连接
conn = pymysql.connect(
host='127.0.0.1', # 与本地数据库建立连接,可以尝试其他电脑的ip地址
port=3306, # 端口可写可不写,默认3306
user='root',
password="111",
database="navicat_test1", # 选择一个库,关键字可以简化为db
charset="utf8mb4"
) # 更多参数可以点进connect的源码去看
3.2 产生游标
cursor = self.conn.cursor(cursor=pymysql.cursors.DictCursor)
# 括号内不填写额外参数数据是元组指定性不强[(), ()] # 让数据自动组织成字典,括号内填写pymysql.cursors.DictCursor指定返回的是[{},{}]
3.3 编写sql
select code from users_v