python 基于PYMYSQL使用MYSQL數(shù)據(jù)庫
在做測試的時候都會用到數(shù)據(jù)庫,今天寫一篇通過python連接MYSQL數(shù)據(jù)庫
什么是MYSQL數(shù)據(jù)庫MySQL是一個關(guān)系型數(shù)據(jù)庫管理系統(tǒng),由瑞典MySQL AB 公司開發(fā),目前屬于 Oracle 旗下產(chǎn)品。MySQL 是最流行的關(guān)系型數(shù)據(jù)庫管理系統(tǒng)之一,在 WEB 應(yīng)用方面,MySQL是最好的 RDBMS (Relational Database Management System,關(guān)系數(shù)據(jù)庫管理系統(tǒng)) 應(yīng)用軟件之一。
什么是PYMYSQLPyMySQL 是在 Python3.x 版本中用于連接 MySQL 服務(wù)器的一個庫,Python2中則使用mysqldb。
PyMySQL 遵循 Python 數(shù)據(jù)庫 API v2.0 規(guī)范,并包含了 pure-Python MySQL 客戶端庫。
PyMySQL安裝pip install pymysqlPyMySQL使用連接數(shù)據(jù)庫
1、首先導(dǎo)入PyMySQL模塊
2、連接數(shù)據(jù)庫(通過connect())
3、創(chuàng)建一個數(shù)據(jù)庫對象 (通過cursor())
4、進(jìn)行對數(shù)據(jù)庫做增刪改查
# coding:utf-8import pymysql# 連接數(shù)據(jù)庫count = pymysql.connect( host = ’xx.xxx.xxx.xx’, # 數(shù)據(jù)庫地址 port = 3306, # 數(shù)據(jù)庫端口號 user=’xxxx’, # 數(shù)據(jù)庫賬號 password=’XXXX’, # 數(shù)據(jù)庫密碼 db = ’test_sll’) # 數(shù)據(jù)庫表名# 創(chuàng)建數(shù)據(jù)庫對象db = count.cursor()查找數(shù)據(jù)
db.fetchone()獲取一條數(shù)據(jù)
db.fetchall()獲取全部數(shù)據(jù)
# coding:utf-8import pymysql# 連接數(shù)據(jù)庫count = pymysql.connect( host = ’xx.xxx.xxx.xx’, # 數(shù)據(jù)庫地址 port = 3306, # 數(shù)據(jù)庫端口號 user=’xxxx’, # 數(shù)據(jù)庫賬號 password=’xxxx’, # 數(shù)據(jù)庫密碼 db = ’test_sll’) # 數(shù)據(jù)庫名稱# 創(chuàng)建數(shù)據(jù)庫對象db = count.cursor()# 寫入SQL語句sql = 'select * from students '# 執(zhí)行sql命令db.execute(sql)# 獲取一個查詢# restul = db.fetchone()# 獲取全部的查詢內(nèi)容restul = db.fetchall()print(restul)db.close()修改數(shù)據(jù)
commit() 執(zhí)行完SQL后需要提交保存內(nèi)容
# coding:utf-8import pymysql# 連接數(shù)據(jù)庫count = pymysql.connect( host = ’xx.xxx.xxx.xx’, # 數(shù)據(jù)庫地址 port = 3306, # 數(shù)據(jù)庫端口號 user=’xxx’, # 數(shù)據(jù)庫賬號 password=’xxx’, # 數(shù)據(jù)庫密碼 db = ’test_sll’) # 數(shù)據(jù)庫表名# 創(chuàng)建數(shù)據(jù)庫對象db = count.cursor()# 寫入SQL語句sql = 'update students set age = ’12’ WHERE id=1'# 執(zhí)行sql命令db.execute(sql)# 保存操作count.commit()db.close()刪除數(shù)據(jù)
# coding:utf-8import pymysql# 連接數(shù)據(jù)庫count = pymysql.connect( host = ’xx.xxx.xxx.xx’, # 數(shù)據(jù)庫地址 port = 3306, # 數(shù)據(jù)庫端口號 user=’xxxx’, # 數(shù)據(jù)庫賬號 password=’xxx’, # 數(shù)據(jù)庫密碼 db = ’test_sll’) # 數(shù)據(jù)庫表名# 創(chuàng)建數(shù)據(jù)庫對象db = count.cursor()# 寫入SQL語句sql = 'delete from students where age = 12'# 執(zhí)行sql命令db.execute(sql)# 保存提交count.commit()db.close()新增數(shù)據(jù)
新增數(shù)據(jù)這里涉及到一個事務(wù)問題,事物機(jī)制可以保證數(shù)據(jù)的一致性,比如插入一個數(shù)據(jù),不會存在插入一半的情況,要么全部插入,要么都不插入
# coding:utf-8import pymysql# 連接數(shù)據(jù)庫count = pymysql.connect( host = ’xx.xxx.xxx.xx’, # 數(shù)據(jù)庫地址 port = 3306, # 數(shù)據(jù)庫端口號 user=’xxxx’, # 數(shù)據(jù)庫賬號 password=’xxx’, # 數(shù)據(jù)庫密碼 db = ’test_sll’) # 數(shù)據(jù)庫表名# 創(chuàng)建數(shù)據(jù)庫對象db = count.cursor()# 寫入SQL語句sql = 'insert INTO students(id,name,age)VALUES (2,’安靜’,’26’)'# 執(zhí)行sql命令db.execute(sql)# 保存提交count.commit()db.close()
到這可以發(fā)現(xiàn)除了查詢不需要保存,其他操作都要提交保存,并且還會發(fā)現(xiàn)刪除,修改,新增,只是修改了SQL,其他的沒什么變化
創(chuàng)建表創(chuàng)建表首先我們先定義下表內(nèi)容的字段
字段名 含義 類型 id id varchar name 姓名 varchar age 年齡 int
# coding:utf-8import pymysql# 連接數(shù)據(jù)庫count = pymysql.connect( host = ’xx.xxx.xxx.xx’, # 數(shù)據(jù)庫地址 port = 3306, # 數(shù)據(jù)庫端口號 user=’xxxx’, # 數(shù)據(jù)庫賬號 password=’xxx’, # 數(shù)據(jù)庫密碼 db = ’test_sll’) # 數(shù)據(jù)庫表名# 創(chuàng)建數(shù)據(jù)庫對象db = count.cursor()# 寫入SQL語句sql = ’CREATE TABLE students (id VARCHAR(255) ,name VARCHAR(255) ,age INT)’# 執(zhí)行sql命令db.execute(sql)db.close()
以上就是python 基于PYMYSQL使用MYSQL數(shù)據(jù)庫的詳細(xì)內(nèi)容,更多關(guān)于python 使用MySQL的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. Python的文本常量與字符串模板之string庫2. SpringBoot+TestNG單元測試的實現(xiàn)3. 利用CSS制作3D動畫4. .Net加密神器Eazfuscator.NET?2023.2?最新版使用教程5. jsp+servlet簡單實現(xiàn)上傳文件功能(保存目錄改進(jìn))6. Springboot 全局日期格式化處理的實現(xiàn)7. Java GZip 基于內(nèi)存實現(xiàn)壓縮和解壓的方法8. 完美解決vue 中多個echarts圖表自適應(yīng)的問題9. 存儲于xml中需要的HTML轉(zhuǎn)義代碼10. JAMon(Java Application Monitor)備忘記
