Python鏈接MySQL數據庫以及語法操做

Time will tell(時間會證實一切).python

一、Python鏈接MySQL數據庫是爲了作什麼

自動化測試時,註冊了一個新用戶,產生了多餘的數據,而後同個帳號下次就沒法註冊了,遇到這種狀況該怎麼辦呢?mysql

自動化測試都有數據準備和數據清理的操做,若是所以用例產生了多餘數據,就須要清理數據,能夠用Pyhthon鏈接Mysql直接刪除多餘數據。面試

 

二、Python鏈接MySQL模塊安裝

第一種安裝方法:sql

在Pycharm—點擊–Terminal—輸入pip install PyMySQL等待完裝完畢便可,如圖所示數據庫

 


第二種安裝方法:網絡

有時候在線安裝第三方模塊的時,會由於網絡緣由而裝不上,那麼咱們就須要手動安裝。學習

一、下載所須要的模塊包
測試

二、解壓該文件

三、將文件名改短,而後放入非C盤且放在根目錄。fetch

四、打開cmd---->E:---->cd xlrd---->python setup.py installspa

五、等待安裝完畢。

六、導入模塊 import xlrd,運行若是沒報錯就說明安裝正常。

三、鏈接MySQL

代碼:

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()

 

二、刪除數據

語法:

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()

 

絮叨

對接口、自動化、軟件測試零基礎入門、python全棧、面試題感興趣能夠加入咱們175317069一塊兒學習,羣內會有不按期測試資料連接發放喔。

相關文章
相關標籤/搜索