LeetCode——整數轉羅馬數字
什麼要做python連線mysql,一般是解決什麼問題的
做自動化測試時候,註冊了一個新使用者,產生了多餘的資料,下次同一個賬號就無法註冊了,這種情況怎麼辦呢?自動化測試都有資料準備和資料清理的操作,如果因此用例產生了多餘資料,就需要清理資料,可以用Pyhthon連線Mysql直接刪除多餘的資料就可以了。
Python3如何連線Mysql呢?PyMySQL是在Py3版本用於連線Mysql
python連線mysql的模組安裝
線上安裝
在Pycharm---點選--Terminal---輸入pip install PyMySQL等待完裝完畢即可,如圖所示
離線安裝
有時候線上安裝第三方模組的時,會因為網路原因總是裝不上,那怎麼辦呢?那就手動安裝
- 下載所需要的模組包
- 解壓該檔案
-
將檔名改短,然後放入非C盤且放在根目錄
-
開啟cmd---->E:---->cd xlrd---->python setup.py install
-
等待完裝完畢
-
匯入模組 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()
執行結果:
查詢語句返回結果是多行的,如何實現
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()
執行結果
查詢資料
查詢單條資料
語法:
select 列名稱 from 表名稱 [查詢條件]
例如要查詢 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()
執行結果
如果對軟體測試、介面測試、自動化測試、技術同行、持續整合、面試經驗交流。感興趣可以進到902061117,群內會有不定期的分享測試資料。
如果文章對你有幫助,麻煩伸出發財小手點個贊,感謝您的支援,你的點贊是我持續更新的動力。