自動化測驗中,經常需要向資料庫添加或洗掉資料,也需要驗證測驗資料和資料庫的資料是否一致,這個時候就需要用Python連接Mysql
安裝PyMySQL
pip install PyMySQL
連接MySql
import pymysql # 打開資料庫連接 db = pymysql.connect("localhost", "root", "111223", "study_date") # 使用 cursor() 方法創建一個游標物件 cursor cursor = db.cursor() # 使用 execute() 方法執行 SQL 查詢 cursor.execute("SELECT VERSION()") # 使用 fetchone() 方法獲取單條資料. data =https://www.cnblogs.com/WeTester/p/ cursor.fetchone() print("Database version : %s " % data) # 關閉資料庫連接 db.close()
資料庫基本操作
增加資料
insert 陳述句可以用來將一行或多行資料插到資料庫表中, 使用的一般形式如下:
insert into 表名 [(列名1, 列名2, 列名3, ...)] values (值1, 值2, 值3, ...);
import pymysql # 打開資料庫連接 db = pymysql.connect("localhost", "root", "111223", "study_date") # 使用cursor()方法獲取操作游標,以字典的方式獲取資料,原資料型別為元組 cursor = db.cursor(cursor=pymysql.cursors.DictCursor) insert_sql = "insert into studys(id, name, age) values(3, '騎著烏龜趕豬', 35)" # 執行sql陳述句 cursor.execute(insert_sql) # 提交到資料庫執行 db.commit() # 關閉資料庫連接 db.close()
洗掉資料
delete 陳述句用于洗掉表中的資料
delete from 表名稱 where 洗掉條件;
import pymysql # 打開資料庫連接 db = pymysql.connect("localhost", "root", "111223", "study_date") # 使用cursor()方法獲取操作游標,以字典的方式獲取資料,原資料型別為元組 cursor = db.cursor(cursor=pymysql.cursors.DictCursor) # SQL 洗掉資料 del_sql = "delete from studys where id=3" # 執行sql陳述句 cursor.execute(del_sql) # 提交到資料庫執行 db.commit() # 關閉資料庫連接 db.close()
修改資料
update 陳述句可用來修改表中的資料
update 表名稱 set 列名稱=新值 where 更新條件;
update studys set age=25 where id=1
import pymysql # 打開資料庫連接 db = pymysql.connect("localhost", "root", "111223", "study_date") # 使用cursor()方法獲取操作游標,以字典的方式獲取資料,原資料型別為元組 cursor = db.cursor(cursor=pymysql.cursors.DictCursor) # SQL 修改資料 updata_sql = "update studys set age=30 where id=2" # 執行sql陳述句 cursor.execute(updata_sql) # 提交到資料庫執行 db.commit() # 關閉資料庫連接 db.close()
查詢資料
語法:
fetchone()獲取一行資料
fetchone()獲多行資料
# 匯入模塊 import pymysql # 打開資料庫連接 資料庫地址 db = pymysql.connect("localhost", "root", "111223", "study_date") # 使用cursor()方法獲取操作游標,以字典的方式獲取資料,原資料型別為元組 cursor = db.cursor(cursor=pymysql.cursors.DictCursor) # 使用 execute()方法執行 SQL 查詢 cursor.execute("select * from studys") # 使用 fetchone() 方法獲取一行資料. data =https://www.cnblogs.com/WeTester/p/ cursor.fetchone() print(data) # 關閉資料庫連接 db.close()
電商專案實戰--判斷主界面商品分類是否與資料庫一致
第1步:獲取界面分類資料
#! /usr/bin/python3 # -*- coding:utf-8 -*- # @FileName: day10.py # @Time : 2020/8/21 9:26 # 公眾號 : 程式員一凡 from selenium import webdriver url = "http://localhost:8080/Shopping/index.jsp" driver = webdriver.Chrome() driver.get(url) # 復數形式定位定位 els = driver.find_elements_by_xpath("/html/body/table[5]/tbody/tr/td[2]/table[1]/tbody/tr/td/a") # 空串列用于存放資料 list = [ ] for i in els: # 獲取text文本然后添加到串列 list.append(i.text) print("界面資料:", list)
第2步:獲取資料庫商品分類資料
db = pymysql.connect(host="localhost", user="root", password="111223", database="db_shopping") cur = db.cursor(cursor=pymysql.cursors.DictCursor) # 從tb_bigtype資料庫中選擇bigName串列 sql = "select bigName from tb_bigtype" cur.execute(sql) db.commit() cur.execute(sql) data = cur.fetchall() db_list = [] print("資料庫資料:",data) for i in data: for j in i.values(): db_list.append(j) print(db_list)
第3步:兩組資料進行斷言
# 反向排序串列 list.reverse() if db_list == list: print("一致") else: print("不一致")
課后練習
-
將電商實戰代碼封裝后再進行斷言
-
將資料庫的操作封裝在一個類中,并且把連接資料庫、增、刪、改、查的操作封裝為方法
文章首發于公眾號程式員一凡,轉載請注明出處!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/233419.html
標籤:其他
上一篇:一、less命令查看日志
下一篇:Python-自動化測驗面試
