python 基于PYMYSQL使用MYSQL數(shù)據(jù)庫(kù)
在做測(cè)試的時(shí)候都會(huì)用到數(shù)據(jù)庫(kù),今天寫(xiě)一篇通過(guò)python連接MYSQL數(shù)據(jù)庫(kù)
什么是MYSQL數(shù)據(jù)庫(kù)
MySQL是一個(gè)關(guān)系型數(shù)據(jù)庫(kù)管理系統(tǒng),由瑞典MySQL AB 公司開(kāi)發(fā),目前屬于 Oracle 旗下產(chǎn)品。MySQL 是最流行的關(guān)系型數(shù)據(jù)庫(kù)管理系統(tǒng)之一,在 WEB 應(yīng)用方面,MySQL是最好的 RDBMS (Relational Database Management System,關(guān)系數(shù)據(jù)庫(kù)管理系統(tǒng)) 應(yīng)用軟件之一。
什么是PYMYSQL
PyMySQL 是在 Python3.x 版本中用于連接 MySQL 服務(wù)器的一個(gè)庫(kù),Python2中則使用mysqldb。
PyMySQL 遵循 Python 數(shù)據(jù)庫(kù) API v2.0 規(guī)范,并包含了 pure-Python MySQL 客戶端庫(kù)。
PyMySQL安裝
pip install pymysql
PyMySQL使用
連接數(shù)據(jù)庫(kù)
1、首先導(dǎo)入PyMySQL模塊
2、連接數(shù)據(jù)庫(kù)(通過(guò)connect())
3、創(chuàng)建一個(gè)數(shù)據(jù)庫(kù)對(duì)象 (通過(guò)cursor())
4、進(jìn)行對(duì)數(shù)據(jù)庫(kù)做增刪改查
# coding:utf-8
import pymysql
# 連接數(shù)據(jù)庫(kù)
count = pymysql.connect(
host = 'xx.xxx.xxx.xx', # 數(shù)據(jù)庫(kù)地址
port = 3306, # 數(shù)據(jù)庫(kù)端口號(hào)
user='xxxx', # 數(shù)據(jù)庫(kù)賬號(hào)
password='XXXX', # 數(shù)據(jù)庫(kù)密碼
db = 'test_sll') # 數(shù)據(jù)庫(kù)表名# 創(chuàng)建數(shù)據(jù)庫(kù)對(duì)象
db = count.cursor()
查找數(shù)據(jù)
db.fetchone()獲取一條數(shù)據(jù)
db.fetchall()獲取全部數(shù)據(jù)
# coding:utf-8
import pymysql
# 連接數(shù)據(jù)庫(kù)
count = pymysql.connect(
host = 'xx.xxx.xxx.xx', # 數(shù)據(jù)庫(kù)地址
port = 3306, # 數(shù)據(jù)庫(kù)端口號(hào)
user='xxxx', # 數(shù)據(jù)庫(kù)賬號(hào)
password='xxxx', # 數(shù)據(jù)庫(kù)密碼
db = 'test_sll') # 數(shù)據(jù)庫(kù)名稱(chēng)
# 創(chuàng)建數(shù)據(jù)庫(kù)對(duì)象
db = count.cursor()
# 寫(xiě)入SQL語(yǔ)句
sql = "select * from students "
# 執(zhí)行sql命令
db.execute(sql)
# 獲取一個(gè)查詢
# restul = db.fetchone()
# 獲取全部的查詢內(nèi)容
restul = db.fetchall()
print(restul)
db.close()
修改數(shù)據(jù)
commit() 執(zhí)行完SQL后需要提交保存內(nèi)容
# coding:utf-8
import pymysql
# 連接數(shù)據(jù)庫(kù)
count = pymysql.connect(
host = 'xx.xxx.xxx.xx', # 數(shù)據(jù)庫(kù)地址
port = 3306, # 數(shù)據(jù)庫(kù)端口號(hào)
user='xxx', # 數(shù)據(jù)庫(kù)賬號(hào)
password='xxx', # 數(shù)據(jù)庫(kù)密碼
db = 'test_sll') # 數(shù)據(jù)庫(kù)表名
# 創(chuàng)建數(shù)據(jù)庫(kù)對(duì)象
db = count.cursor()
# 寫(xiě)入SQL語(yǔ)句
sql = "update students set age = '12' WHERE id=1"
# 執(zhí)行sql命令
db.execute(sql)
# 保存操作
count.commit()
db.close()
刪除數(shù)據(jù)
# coding:utf-8
import pymysql
# 連接數(shù)據(jù)庫(kù)
count = pymysql.connect(
host = 'xx.xxx.xxx.xx', # 數(shù)據(jù)庫(kù)地址
port = 3306, # 數(shù)據(jù)庫(kù)端口號(hào)
user='xxxx', # 數(shù)據(jù)庫(kù)賬號(hào)
password='xxx', # 數(shù)據(jù)庫(kù)密碼
db = 'test_sll') # 數(shù)據(jù)庫(kù)表名
# 創(chuàng)建數(shù)據(jù)庫(kù)對(duì)象
db = count.cursor()
# 寫(xiě)入SQL語(yǔ)句
sql = "delete from students where age = 12"
# 執(zhí)行sql命令
db.execute(sql)
# 保存提交
count.commit()
db.close()
新增數(shù)據(jù)
新增數(shù)據(jù)這里涉及到一個(gè)事務(wù)問(wèn)題,事物機(jī)制可以保證數(shù)據(jù)的一致性,比如插入一個(gè)數(shù)據(jù),不會(huì)存在插入一半的情況,要么全部插入,要么都不插入
# coding:utf-8
import pymysql
# 連接數(shù)據(jù)庫(kù)
count = pymysql.connect(
host = 'xx.xxx.xxx.xx', # 數(shù)據(jù)庫(kù)地址
port = 3306, # 數(shù)據(jù)庫(kù)端口號(hào)
user='xxxx', # 數(shù)據(jù)庫(kù)賬號(hào)
password='xxx', # 數(shù)據(jù)庫(kù)密碼
db = 'test_sll') # 數(shù)據(jù)庫(kù)表名
# 創(chuàng)建數(shù)據(jù)庫(kù)對(duì)象
db = count.cursor()
# 寫(xiě)入SQL語(yǔ)句
sql = "insert INTO students(id,name,age)VALUES (2,'安靜','26')"
# 執(zhí)行sql命令
db.execute(sql)
# 保存提交
count.commit()
db.close()
到這可以發(fā)現(xiàn)除了查詢不需要保存,其他操作都要提交保存,并且還會(huì)發(fā)現(xiàn)刪除,修改,新增,只是修改了SQL,其他的沒(méi)什么變化
創(chuàng)建表
創(chuàng)建表首先我們先定義下表內(nèi)容的字段
| 字段名 | 含義 | 類(lèi)型 |
| id | id | varchar |
| name | 姓名 | varchar |
| age | 年齡 | int |
# coding:utf-8
import pymysql
# 連接數(shù)據(jù)庫(kù)
count = pymysql.connect(
host = 'xx.xxx.xxx.xx', # 數(shù)據(jù)庫(kù)地址
port = 3306, # 數(shù)據(jù)庫(kù)端口號(hào)
user='xxxx', # 數(shù)據(jù)庫(kù)賬號(hào)
password='xxx', # 數(shù)據(jù)庫(kù)密碼
db = 'test_sll') # 數(shù)據(jù)庫(kù)表名
# 創(chuàng)建數(shù)據(jù)庫(kù)對(duì)象
db = count.cursor()
# 寫(xiě)入SQL語(yǔ)句
sql = 'CREATE TABLE students (id VARCHAR(255) ,name VARCHAR(255) ,age INT)'
# 執(zhí)行sql命令
db.execute(sql)
db.close()
以上就是python 基于PYMYSQL使用MYSQL數(shù)據(jù)庫(kù)的詳細(xì)內(nèi)容,更多關(guān)于python 使用MySQL的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- 利用python中pymysql操作MySQL數(shù)據(jù)庫(kù)的新手指南
- Python接口自動(dòng)化淺析pymysql數(shù)據(jù)庫(kù)操作流程
- python使用pymysql模塊操作MySQL
- pymysql實(shí)現(xiàn)增刪改查的操作指南(python)
- python pymysql庫(kù)的常用操作
- Python pymysql模塊安裝并操作過(guò)程解析
- python數(shù)據(jù)庫(kù)操作mysql:pymysql、sqlalchemy常見(jiàn)用法詳解
- 在python中使用pymysql往mysql數(shù)據(jù)庫(kù)中插入(insert)數(shù)據(jù)實(shí)例
- Python使用pymysql模塊操作mysql增刪改查實(shí)例分析
- python之pymysql模塊簡(jiǎn)單應(yīng)用示例代碼
- wxpython+pymysql實(shí)現(xiàn)用戶登陸功能
- 在Python中使用MySQL--PyMySQL的基本使用方法
- Python 中使用 PyMySQL模塊操作數(shù)據(jù)庫(kù)的方法
- 使用python連接mysql數(shù)據(jù)庫(kù)之pymysql模塊的使用
- Python pymysql操作MySQL詳細(xì)
相關(guān)文章
使用pytorch實(shí)現(xiàn)論文中的unet網(wǎng)絡(luò)
這篇文章主要介紹了使用pytorch實(shí)現(xiàn)論文中的unet網(wǎng)絡(luò),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-06-06
python基礎(chǔ)教程之對(duì)象和類(lèi)的實(shí)際運(yùn)用
這篇文章主要介紹了python基礎(chǔ)教程之對(duì)象和類(lèi)的實(shí)際運(yùn)用,本文講解對(duì)象和類(lèi)的一方法技巧,例如屬性、內(nèi)置方法、self關(guān)鍵字的運(yùn)用等,需要的朋友可以參考下2014-08-08
Tensorflow2.1實(shí)現(xiàn)文本中情感分類(lèi)實(shí)現(xiàn)解析
這篇文章主要為大家介紹了Tensorflow2.1實(shí)現(xiàn)文本中情感分類(lèi)實(shí)現(xiàn)解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
Python使用matplotlib填充圖形指定區(qū)域代碼示例
這篇文章主要介紹了Python使用matplotlib填充圖形指定區(qū)域代碼示例,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-01-01
利用jupyter網(wǎng)頁(yè)版本進(jìn)行python函數(shù)查詢方式
這篇文章主要介紹了利用jupyter網(wǎng)頁(yè)版本進(jìn)行python函數(shù)查詢方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04

