Python實現(xiàn)連接MySql數(shù)據(jù)庫及增刪改查操作詳解
本文實例講述了Python實現(xiàn)連接MySql數(shù)據(jù)庫及增刪改查操作。分享給大家供大家參考,具體如下:
在本文中介紹 Python3 使用PyMySQL連接數(shù)據(jù)庫,并實現(xiàn)簡單的增刪改查。(注意是python3)
1、安裝PyMySQL
PyMySQL 是在 Python3.x 版本中用于連接 MySQL 服務(wù)器的一個庫,Python2中則使用mysqldb。PyMySQL 遵循 Python 數(shù)據(jù)庫 API v2.0 規(guī)范,并包含了 pure-Python MySQL 客戶端庫。在使用 PyMySQL 之前,我們需要確保 PyMySQL 已安裝。
① 使用pip命令安裝
pip install PyMySQL

② 如果你的系統(tǒng)不支持 pip 命令,可以使用以下git方式安裝
//使用git下載安裝包 $ git clone https://github.com/PyMySQL/PyMySQL $ cd PyMySQL/ $ python3 setup.py install
2、Python連接MySql數(shù)據(jù)庫
連接數(shù)據(jù)庫前,請先確認以下事項:
Ⅰ 在你的機子上已經(jīng)安裝了 Python MySQLdb 模塊。
Ⅱ 您已經(jīng)創(chuàng)建了數(shù)據(jù)庫 test
Ⅲ 連接數(shù)據(jù)庫test使用的用戶名為 root,密碼為 root,你可以可以自己設(shè)定或者直接使用root用戶名及其密碼。
# *===================================*
# * Created by Zhihua_w.
# * Author: Wei ZhiHua
# * Date: 2017/1/10 0003
# * Time: 下午 2:28
# * Project: PYTHON STUDY
# * Power: DATABASE
# *===================================*
import pymysql
# 打開數(shù)據(jù)庫連接(ip/數(shù)據(jù)庫用戶名/登錄密碼/數(shù)據(jù)庫名)
db = pymysql.connect("localhost", "root", "root", "test")
# 使用 cursor() 方法創(chuàng)建一個游標對象 cursor
cursor = db.cursor()
# 使用 execute() 方法執(zhí)行 SQL 查詢
cursor.execute("SELECT VERSION()")
# 使用 fetchone() 方法獲取單條數(shù)據(jù).
data = cursor.fetchone()
print("Database version : %s " % data)
# 關(guān)閉數(shù)據(jù)庫連接
db.close()
3、Python操作MySql數(shù)據(jù)庫實現(xiàn)增刪改查
① 數(shù)據(jù)庫插入操作
# *===================================*
# * Created by Zhihua_w.
# * Author: Wei ZhiHua
# * Date: 2017/1/10 0004
# * Time: 下午 2:32
# * Project: PYTHON STUDY
# * Power: DATABASE
# *===================================*
import pymysql
# 打開數(shù)據(jù)庫連接(ip/數(shù)據(jù)庫用戶名/登錄密碼/數(shù)據(jù)庫名)
db = pymysql.connect("localhost", "root", "root", "test")
# 使用 cursor() 方法創(chuàng)建一個游標對象 cursor
cursor = db.cursor()
# SQL 插入語句
sql = """INSERT INTO user(name)
VALUES ('Mac')"""
try:
# 執(zhí)行sql語句
cursor.execute(sql)
# 提交到數(shù)據(jù)庫執(zhí)行
db.commit()
except:
# 如果發(fā)生錯誤則回滾
db.rollback()
# 關(guān)閉數(shù)據(jù)庫連接
db.close()
② 數(shù)據(jù)庫查詢
# *===================================*
# * Created by Zhihua_w.
# * Author: Wei ZhiHua
# * Date: 2017/1/10 0005
# * Time: 下午 2:39
# * Project: PYTHON STUDY
# * Power: DATABASE
# *===================================*
import pymysql
# 打開數(shù)據(jù)庫連接(ip/數(shù)據(jù)庫用戶名/登錄密碼/數(shù)據(jù)庫名)
db = pymysql.connect("localhost", "root", "root", "test")
# 使用 cursor() 方法創(chuàng)建一個游標對象 cursor
cursor = db.cursor()
# SQL 查詢語句
sql = "SELECT * FROM user"
try:
# 執(zhí)行SQL語句
cursor.execute(sql)
# 獲取所有記錄列表
results = cursor.fetchall()
for row in results:
id = row[0]
name = row[1]
# 打印結(jié)果
print("id=%s,name=%s" % \
(id, name))
except:
print("Error: unable to fecth data")
# 關(guān)閉數(shù)據(jù)庫連接
db.close()
③ 數(shù)據(jù)庫更新
# *===================================*
# * Created by Zhihua_w.
# * Author: Wei ZhiHua
# * Date: 2017/1/10 0005
# * Time: 下午 2:39
# * Project: PYTHON STUDY
# * Power: DATABASE
# *===================================*
import pymysql
# 打開數(shù)據(jù)庫連接(ip/數(shù)據(jù)庫用戶名/登錄密碼/數(shù)據(jù)庫名)
db = pymysql.connect("localhost", "root", "root", "test")
# 使用 cursor() 方法創(chuàng)建一個游標對象 cursor
cursor = db.cursor()
# SQL 更新語句
sql = "UPDATE user SET name = 'Bob' WHERE id = 1"
try:
# 執(zhí)行SQL語句
cursor.execute(sql)
# 提交到數(shù)據(jù)庫執(zhí)行
db.commit()
except:
# 發(fā)生錯誤時回滾
db.rollback()
# 關(guān)閉數(shù)據(jù)庫連接
db.close()
④ 數(shù)據(jù)庫刪除
# *===================================*
# * Created by Zhihua_w.
# * Author: Wei ZhiHua
# * Date: 2017/1/10 0006
# * Time: 下午 2:49
# * Project: PYTHON STUDY
# * Power: DATABASE
# *===================================*
import pymysql
# 打開數(shù)據(jù)庫連接(ip/數(shù)據(jù)庫用戶名/登錄密碼/數(shù)據(jù)庫名)
db = pymysql.connect("localhost", "root", "root", "test")
# 使用 cursor() 方法創(chuàng)建一個游標對象 cursor
cursor = db.cursor()
# SQL 刪除語句
sql = "DELETE FROM user WHERE id = 1"
try:
# 執(zhí)行SQL語句
cursor.execute(sql)
# 提交修改
db.commit()
except:
# 發(fā)生錯誤時回滾
db.rollback()
# 關(guān)閉數(shù)據(jù)庫連接
db.close()
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python常見數(shù)據(jù)庫操作技巧匯總》、《Python數(shù)學(xué)運算技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設(shè)計有所幫助。
相關(guān)文章
連接pandas以及數(shù)組轉(zhuǎn)pandas的方法
今天小編就為大家分享一篇連接pandas以及數(shù)組轉(zhuǎn)pandas的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-06-06
python之線程池map()方法傳遞多參數(shù)list
這篇文章主要介紹了python之線程池map()方法傳遞多參數(shù)list問題,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03

