Time will tell(時間會證明一切).
1、Python連接MySQL資料庫是為了做什么
自動化測驗時,注冊了一個新用戶,產生了多余的資料,然后同個賬號下次就無法注冊了,遇到這種情況該怎么辦呢?
自動化測驗都有資料準備和資料清理的操作,如果因此用例產生了多余資料,就需要清理資料,可以用Pyhthon連接Mysql直接洗掉多余資料,

2、Python連接MySQL模塊安裝
第一種安裝方法:
在Pycharm—點擊–Terminal—輸入pip install PyMySQL等待完裝完畢即可,如圖所示

第二種安裝方法:
有時候在線安裝第三方模塊的時,會因為網路原因而裝不上,那么我們就需要手動安裝,
1、下載所需要的模塊包
2、解壓該檔案
3、將檔案名改短,然后放入非C盤且放在根目錄,
4、打開cmd---->E:---->cd xlrd---->python setup.py install
5、等待安裝完畢,
6、匯入模塊 import xlrd,運行如果沒報錯就說明安裝正常,
3、連接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/flowToken1024532/p/ cursor.fetchone() print("Database version : %s " % data) # 關閉資料庫連接 db.close()
運行結果:
4、資料庫查詢
1、查詢一行資料,語法:
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 =https://www.cnblogs.com/flowToken1024532/p/ cursor.fetchone() 16 print(data) 17 18 # 關閉資料庫連接 19 db.close()
運行結果:
2、如何使用查詢陳述句回傳多行結果
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 =https://www.cnblogs.com/flowToken1024532/p/ cursor.fetchall() 15 print(data) 16 17 # 關閉資料庫連接 18 db.close()
運行結果:
5、資料庫的基本增刪查改操作
1、添加資料
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 =https://www.cnblogs.com/flowToken1024532/p/ cursor.fetchall() 14 print(data) # 關閉資料庫連接 db.close()
2、洗掉資料
語法:
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 =https://www.cnblogs.com/flowToken1024532/p/ cursor.fetchall() print(data) except: # 如果發生錯誤則回滾 db.rollback() # 關閉資料庫連接 db.close()
3、修改資料
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 =https://www.cnblogs.com/flowToken1024532/p/ cursor.fetchall() print(data) except: # 如果發生錯誤則回滾 db.rollback() # 關閉資料庫連接 db.close()
絮叨
對介面、自動化、軟體測驗零基礎入門、python全堆疊、面試題感興趣可以加入我們175317069一起學習,群內會有不定期測驗資料鏈接發放喔,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/70497.html
標籤:其他
