pymysql實現(xiàn)增刪改查的操作指南(python)
1.安裝pymysql:pip install pymysql (在命令行窗口中執(zhí)行)
2.卸載pymysql:pip uninstall pymysql (在命令行窗口中執(zhí)行)
數(shù)據(jù)庫的連接
需要注意的是port是不用引號括起來 charset是utf8不是utf-8
# 獲取數(shù)據(jù)庫連接對象
connection = pymysql.connect(host='localhost', port=3306, user='root', passwd='2732195202', db='book', charset='utf8')
# 獲取一個游標
driver = connection.cursor()
# 執(zhí)行一條sql
driver.execute("select version()")
# 獲取執(zhí)行sql的返回值
resultData=driver.fetchall()
print(resultData)
# 關閉數(shù)據(jù)庫
connection.close()
創(chuàng)建數(shù)據(jù)庫表
import pymysql
#獲取數(shù)據(jù)庫連接對象
connection = pymysql.connect(host='localhost', port=3306, user='root', passwd='2732195202', db='book', charset='utf8')
#獲取一個游標
driver=connection.cursor()
# 如果該數(shù)據(jù)庫存在就刪除
driver.execute("drop table if exists t_emp ")
# 定義sql語句
sql=""" CREATE TABLE `t_emp` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主鍵',
`department` varchar(20) DEFAULT NULL COMMENT '部門',
`salary` decimal(10,2) DEFAULT NULL COMMENT '工資',
`age` int(11) DEFAULT NULL COMMENT '年齡',
`sex` varchar(4) DEFAULT NULL COMMENT '性別',
PRIMARY KEY (`id`)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
"""
# 執(zhí)行sql
driver.execute(sql)
# 關閉數(shù)據(jù)連接
connection.close()
向數(shù)據(jù)庫中添加數(shù)據(jù)
1.需要注意的是規(guī)范sql,該寫的字段都寫上,不使用默認對應
2.提交事務的對象是數(shù)據(jù)庫連接對象,而不是游標對象
3.pycharm連接mysql數(shù)據(jù)時,如果連接驅(qū)動是高版本,需要加上時區(qū),jdbc:mysql://localhost/book?serverTimezone=GMT%2B8
4.如果主鍵是自動遞增,則不能手動指定值,不能寫該字段,讓其自增長
# 獲取數(shù)據(jù)庫連接對象
connection=pymysql.connect(host='localhost',port=3306,user='root',passwd='2732195202',db='book',charset='utf8')
# 獲取一個游標
driver=connection.cursor()
# 定義sql語句
sql=""" insert into t_emp(name,department,salary,age,sex)
values("tom","開發(fā)部",8000,25,"男"), ("tom","開發(fā)部",8000,25,"男")
"""
# 嘗試捕捉錯誤
try:
# 執(zhí)行SQL,并返回收影響行數(shù)
result=driver.execute(sql)
# 提交事務
connection.commit()
print("sql(insert)->error")
except:
# 如果發(fā)生錯誤 則回滾事務
print("sql(insert)->error")
driver.rollback()
# 關閉數(shù)據(jù)庫連接
connection.close()
修改表中的數(shù)據(jù)
注意點:在操作數(shù)據(jù)庫之前,需要確認是否獲取連接數(shù)據(jù)庫成功,并且選中了數(shù)庫
2.卸載第三方庫:pip uninstall pymysql
#獲取數(shù)據(jù)庫連接對象 autocommit=True:設置數(shù)據(jù)庫自動提交
connection=pymysql.connect(host="localhost",port=3306,user='root',passwd='2732195202',db='book',charset='utf8',autocommit=True)
# 獲取游標對象
driver=connection.cursor()
# 定義sql
sql="update t_emp set salary=%s,name=%s where id=%s;"
# 如果sql錯誤就執(zhí)行回滾操作,成功就提交
try:
# 執(zhí)行sql,并且返回影響的行數(shù)
result=driver.execute(sql,[6000,"admin",19])
connection.commit()
print("sql(update)->success")
except:
print("sql(update)->error")
connection.rollback()
# 關閉數(shù)據(jù)庫連接對象
connection.close()
查詢數(shù)據(jù)
1.項目中的.py文件不能和python庫中的文件進行沖突,否則會出現(xiàn)異常
# 獲取數(shù)據(jù)庫連接對象
connection=pymysql.connect(host='localhost',port=3306,user='root',passwd='2732195202',db='book',charset='utf8')
# 獲取一個游標對象
driver=connection.cursor()
#定義sql
sql="select id, name, department, salary, age, sex from t_emp where id>%s and sex=%s"
# 只能獲取一次,獲取多次的時候會獲取到null 如果是多個參數(shù),需要傳遞一個元組
try:
driver.execute(sql,(1,"女"))
# 獲取所有的查詢結(jié)果 返回一個元組
resultAll=driver.fetchall()
print("resultAll:", resultAll)
# 獲取2條數(shù)據(jù)
resultTwo=driver.fetchmany(2)
print("resultTwo:", resultTwo)
# 獲取一條數(shù)據(jù)
resultOne=driver.fetchone()
print("resultThree:", resultOne)
print("sql(select)->success")
except:
connection.rollback()
print("sql(select)->error")
# 關閉數(shù)據(jù)庫連接
connection.close()
刪除表中的記錄
import pymysql
# 獲取數(shù)據(jù)庫連接對象
connection = pymysql.connect(host='localhost', port=3306, user='root', passwd='2732195202', db='book', charset='utf8')
# 獲取一個游標
driver = connection.cursor()
# 定義sql
sql="delete from t_emp where id=%s"
try:
# 執(zhí)行一條sql
driver.execute(sql, (21))
# 提交事務
connection.commit()
print("sql(delete)->success")
except Exception as e:
# 回滾事務
connection.rollback()
print("sql(delete)->error")
print(e)
#關閉數(shù)據(jù)庫連接
connection.close()
事務操作

提交事務: connection.commit()
回滾事務: connection.rollback()
總結(jié)
到此這篇關于pymsql實現(xiàn)增刪改查(python)的文章就介紹到這了,更多相關pymsql增刪改查內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
集調(diào)試共享及成本控制Prompt工具PromptLayer使用指南
這篇文章主要介紹了集調(diào)試共享及成本控制Prompt工具PromptLayer使用指南,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-03-03
python環(huán)境功能強大的pip-audit安全漏洞掃描工具
這篇文章主要為大家介紹了python環(huán)境中功能強大的pip-audit安全漏洞掃描工具的功能介紹及安裝使用說明,有需要的朋友可以借鑒參考下,希望能夠有所幫助2022-02-02
Python中用try-except-finally處理異常問題
這篇文章主要介紹了Python中用try-except-finally處理異常問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12
python多版本工具miniconda的配置優(yōu)化實現(xiàn)
通過Miniconda,您可以輕松地創(chuàng)建和管理多個Python環(huán)境,同時確保每個環(huán)境具有所需的依賴項和軟件包,本文主要介紹了python多版本工具miniconda的配置優(yōu)化實現(xiàn),感興趣的可以了解一下2024-01-01
python opencv 找出圖像中的最大輪廓并填充(生成mask)
這篇文章主要介紹了python opencv 找出圖像中的最大輪廓并填充(生成mask),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-03-03

