做自动化测试时候,注册了一个新用户,产生了多余的数据,下次同一个账号就无法注册了,这种情况怎么办呢?自动化测试都有数据准备和数据清理的操作,如果因此用例产生了多余数据,就需要清理数据,可以用Pyhthon连接Mysql直接删除多余的数据就可以了。
Python3如何连接Mysql呢?PyMySQL是在Py3版本用于连接Mysql
在Pycharm---点击--Terminal---输入pip install PyMySQL等待完装完毕即可,如图所示
有时候在线安装第三方模块的时,会因为网络原因总是装不上,那怎么办呢?那就手动安装
第一步:下载所需要的模块包
第二步:解压该文件
第三步:将文件名改短,然后放入非C盘且放在根目录
第四步:打开cmd---->E:---->cd xlrd---->python setup.py install
第五步:等待完装完毕
第六步:导入模块 import xlrd,运行如果没报错就说明安装正常
import pymysql
# 打开数据库连接 db = pymysql.connect("localhost", "root", "111223", "study_date") # 使用 cursor() 方法创建一个游标对象 cursor cursor = db.cursor() # 使用 execute() 方法执行 SQL 查询 cursor.execute("SELECT VERSION()") # 使用 fetchone() 方法获取单条数据. data = cursor.fetchone() print("Database version : %s " % data) # 关闭数据库连接 db.close()
运行结果:
语法:
select 列名称 from 表名称 [查询条件]
# 查询表里所有内容
select * from studys
例如要查询 students 表中所有学生的名字和年龄, 输入语句
select name, age from studys
fetchone()获取一行数据
1 # 导入模块 2 import pymysql 3 4 # 打开数据库连接 数据库地址 5 db = pymysql.connect("localhost", "root", "111223", "study_date") 6 7 # 使用 cursor() 方法创建一个游标对象 cursor 8 cursor = db.cursor() 9 10 # 使用 execute()方法执行 SQL 查询 11 # 通配符,意思是查询表里所有内容 12 cursor.execute("select * from studys") 13 14 # 使用 fetchone() 方法获取一行数据. 15 data = cursor.fetchone() 16 print(data) 17 18 # 关闭数据库连接 19 db.close()
运行结果:
fetchall()获取所有数据
1 # 导入模块,固定写法 2 import pymysql 3 4 # 打开数据库连接 数据库地址 5 db = pymysql.connect("localhost", "root", "111223", "study_date") 6 7 # 使用 cursor() 方法创建一个游标对象 cursor 8 cursor = db.cursor() 9 10 # 使用 execute() 方法执行 SQL 查询 11 cursor.execute("select * from studys") 12 13 # 使用 fetchall() 方法获取所有数据.以元组形式返回 14 data = cursor.fetchall() 15 print(data) 16 17 # 关闭数据库连接 18 db.close()
运行结果:
语法:
insert 语句可以用来将一行或多行数据插到数据库表中, 使用的一般形式如下:
insert [into] 表名 [(列名1, 列名2, 列名3, ...)] values (值1, 值2, 值3, ...);
其中 [ ] 内的内容是可选的, 例如, 要给study_date数据库中的 studys 表插入一条记录, 执行语句:
insert into studys values(3, '骑着乌龟赶猪', 30);
1 import pymysql 2 3 # 打开数据库连接 4 db = pymysql.connect("localhost", "root", "111223", "study_date") 5 # 使用cursor()方法获取操作游标 6 cursor = db.cursor() 7 insert_sql = 8 # 执行sql语句 9 cursor.execute("insert into studys(id, name, age) values(3, '骑着乌龟赶猪', 35)") 10 # 提交到数据库执行 11 db.commit() cursor.execute("select * from studys") 12 # 查看表里所有数据 13 data = cursor.fetchall() 14 print(data) # 关闭数据库连接 db.close()
运行结果:
再运行一次上以代码,运行后报错,两个重要错误信息
1、错误在哪一行
2、这个错误原因,主键冲突
为防止插入数据时出现异常,所以加上try...except
1 import pymysql 2 3 # 打开数据库连接 4 db = pymysql.connect("localhost", "root", "111223", "study_date") 5 6 # 使用cursor()方法获取操作游标 7 cursor = db.cursor() 8 insert_sql = "insert into studys(id, name, age) values(3, '骑着乌龟赶猪', 35)" 9 try: 10 # 执行sql语句 11 cursor.execute(insert_sql) 12 # 提交到数据库执行 13 db.commit() 14 cursor.execute("select * from studys") 15 # 查看表里所有数据 16 data = cursor.fetchall() 17 print(data) 18 except: 19 print("数据插入失败,请查检try语句里的代码") 20 # 关闭数据库连接 21 # 如果想知道报了啥错,可以主动抛出异常 22 # raise 23 db.close()
运行结果:
delete 语句用于删除表中的数据
语法:
delete from 表名称 where 删除条件;
import pymysql # 打开数据库连接 db = pymysql.connect("localhost", "root", "111223", "study_date") # 使用cursor()方法获取操作游标 cursor = db.cursor() check_sql = 'select * from studys' # SQL 删除数据 del_sql = "delete from studys where id=3" try: # 执行sql语句 cursor.execute(del_sql) # 提交到数据库执行 db.commit() cursor.execute(check_sql) # 查看表里所有数据 data = cursor.fetchall() print(data) except: # 如果发生错误则回滚 db.rollback() # 关闭数据库连接 db.close()
运行结果:
update 语句可用来修改表中的数据,
语法:
update 表名称 set 列名称=新值 where 更新条件;
import pymysql # 打开数据库连接 db = pymysql.connect("localhost", "root", "111223", "study_date") # 使用cursor()方法获取操作游标 cursor = db.cursor() check_sql = 'select * from studys' # SQL 修改数据 updata_sql = "update studys set age=30 where id=2" try: # 执行sql语句 cursor.execute(updata_sql) # 提交到数据库执行 db.commit() cursor.execute(check_sql) # 查看表里所有数据 data = cursor.fetchall() print(data) except: # 如果发生错误则回滚 db.rollback() # 关闭数据库连接 db.close()
运行结果