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