Python - 操作 MySQL 資料庫
阿新 • • 發佈:2021-08-15
Python DB-API 規範
- Python 所有的資料庫介面程式都在一定程度上遵守 Python DB-API 規範
- Python DB-API 是一個規範,它定義了一系列必須的物件和資料庫存取方式,以便為各種各樣的底層資料庫系統和多種多樣的資料庫介面程式提供一致的訪問介面
- 在沒有 Python DB-API 之前,各資料庫之間的應用介面非常混亂,實現各不相同
- 如果專案需要更換資料庫時,則需要做大量的修改,非常不便
- Python DB-API 的出現就是為了解決這樣的問題
- 由於 Python DB-API 為不同的資料庫提供了一致的訪問介面, 在不同的資料庫之間移植程式碼成為一件輕鬆的事
什麼是 PyMySQL?
PyMySQL 是在 Python3.x 版本中用於連線 MySQL 伺服器的一個庫,Python2 中則使用 mysqldb
安裝
pip3 install PyMySQL
完整的簡單小栗子
#!usr/bin/env python # -*- coding:utf-8 _*- """ # author: 小菠蘿測試筆記 # blog: https://www.cnblogs.com/poloyy/ # time: 2021/8/12 3:06 下午 # file: 連線mysql.py """ import pymysql db = pymysql.connect( host='localhost', port=3306, user='root', password='1234567890', db='MockServer', charset='utf8' ) # 使用 cursor() 方法建立一個遊標物件 cursor cursor = db.cursor() # 使用 execute() 方法執行 SQL 查詢 cursor.execute("select * from api") # 使用 fetchone() 方法獲取單條資料. data = cursor.fetchone() print("data is : %s" % data) # 關閉資料庫連線 db.close()
訪問資料庫
pymysql.connect( host = 'localhost', port = 3306, user = 'root', password = '123456', db ='MockServer', charset = 'utf8' )
connect 方法生成一個 connect 物件, 通過這個物件來訪問資料庫
connect 方法的引數
引數 | 功能 |
---|---|
user | 訪問資料庫的使用者 |
password | 訪問資料庫的密碼 |
host | Mysql 資料庫服務所在的主機 |
port | Mysql 資料庫服務的埠號,預設值為 3306 |
db | 資料庫名 |
charset | 字元編碼 |
connect 物件
- 使用 connect() 方法與資料庫連線成功後,connect() 方法返回一個 connect() 物件
- 與資料庫進行通訊時, 向 connect 物件傳送 SQL 查詢命令, 並 connect 物件接收 SQL 查詢結果
常用方法
方法 | 功能 |
---|---|
close() | 關閉資料庫連線 |
commit() | 提交當前事務 |
rollback() | 取消當前事務 |
cursor() | 建立一個遊標物件用於執行 SQL 查詢命令 |
cursor 物件
cursor 物件用於執行 SQL 命令和得到 SQL 查詢結果
常用方法
方法 | 功能 |
---|---|
close() | 關閉遊標物件 |
execute() | 執行一個數據庫查詢或命令 |
fetchone() | 返回結果集的下一行 |
fetchall() | 返回結果集中所有行 |
建立資料庫
db = pymysql.connect( host='localhost', port=3306, user='root', password='1234567890', db='MockServer', charset='utf8' ) # 使用 cursor() 方法建立一個遊標物件 cursor cursor = db.cursor() sql = """SET character_set_database=utf8; SET character_set_server=utf8; DROP DATABASE IF EXISTS school; CREATE DATABASE school; USE school;""" lists = sql.split("\n") for i in lists: cursor.execute(i) create_sql = """ CREATE TABLE students( sno VARCHAR(32), name VARCHAR(32), age INT ); """ cursor.execute(create_sql) insert_sql = """ INSERT INTO students(sno, name, age) VALUES ('1', '張三', '20'); """ cursor.execute(insert_sql) db.commit() db.close()
查詢資料
db = pymysql.connect( host='localhost', port=3306, user='root', password='1234567890', db='school', charset='utf8' ) # 使用 cursor() 方法建立一個遊標物件 cursor cursor = db.cursor()
fetchall
sql = "select * from students;" rows = cursor.execute(sql) # 記錄數 print("there are %d students" % rows) # 可迭代物件 students = cursor.fetchall()
# 迴圈輸出 for i in students: print(i) # 輸出結果 there are 1 students ('1', '張三', 20)
fetchone
# 查詢資料 - fetchone sql = "select * from students;" rows = cursor.execute(sql) # 根據記錄數迴圈 for i in range(rows): student = cursor.fetchone() print(student) # 輸出結果 ('1', '張三', 20)
fetchmany
可以自定義返回多少條記錄數# 查詢資料 - fetchmany sql = "select * from students;" rows = cursor.execute(sql) # 可迭代物件 students = cursor.fetchmany(3) # 迴圈結果集 for i in students: print(i) # 輸出結果 ('100', '小菠蘿', 24)
增加資料
db = pymysql.connect( host='localhost', port=3306, user='root', password='1234567890', db='school', charset='utf8' ) # 使用 cursor() 方法建立一個遊標物件 cursor cursor = db.cursor() # 增加資料 sno = 100 name = "小菠蘿" age = 24 sql = 'insert into students(sno,name,age) VALUES("%s", "%s", %d)' % (sno, name, age) # 執行 insert sql rows = cursor.execute(sql) # 檢視 insert 語句返回結果,其實就是執行成功了多少條資料 print('Insert %d students' % rows) # 只有呼叫了 commit 方法才能將資料落盤,即提交 insert 操作 db.commit() # 輸出結果 Insert 1 students
修改資料
db = pymysql.connect( host='localhost', port=3306, user='root', password='1234567890', db='school', charset='utf8' ) # 使用 cursor() 方法建立一個遊標物件 cursor cursor = db.cursor() # 更新資料 sno = 10 name = "小菠蘿" age = 44 sql = 'UPDATE students SET name="%s", age=%d WHERE sno="%s"' % (name, age, sno) # 執行 update sql rows = cursor.execute(sql) # 返回成功修改記錄的條數 print('update %d students' % rows) # 呼叫 commit,才會將 update 操作提交 db.commit() # 輸出結果 update 1 students
刪除資料
db = pymysql.connect( host='localhost', port=3306, user='root', password='1234567890', db='school', charset='utf8' ) # 使用 cursor() 方法建立一個遊標物件 cursor cursor = db.cursor() # 更新資料 sno = 10 name = "小菠蘿" age = 44 sql = 'DELETE FROM students' # 執行 delete sql rows = cursor.execute(sql) # 返回成功修改記錄的條數 print('delete %d students' % rows) # 呼叫 commit,才會將 delete 操作提交 db.commit() # 輸出結果 delete 2 students