Python連接數(shù)據(jù)庫學(xué)習(xí)之DB-API詳解
前言
大家都知道在Python中如果要連接數(shù)據(jù)庫,不管是MySQL、SQL Server、PostgreSQL亦或是SQLite,使用時都是采用游標的方式,所以就不得不學(xué)習(xí)Python DB-API。
Python所有的數(shù)據(jù)庫接口程序都在一定程度上遵守 Python DB-API 規(guī)范。DB-API定義了一系列必須的對象和數(shù)據(jù)庫存取方式,以便為各種底層數(shù)據(jù)庫系統(tǒng)和多種多樣的數(shù)據(jù)庫接口程序提供一致的訪問接口。由于DB-API 為不同的數(shù)據(jù)庫提供了一致的訪問接口, 在不同的數(shù)據(jù)庫之間移植代碼成為一件輕松的事情。
Python連接數(shù)據(jù)庫流程:

使用connect創(chuàng)建connection連接
connect 方法生成一個 connect 對象, 我們通過這個對象來訪問數(shù)據(jù)庫。符合標準的模塊都會實現(xiàn) connect 方法。
connect 函數(shù)的參數(shù)如下所示:
- user Username
- password Password
- host Hostname
- database Database name
- dsn Data source name
數(shù)據(jù)庫連接參數(shù)可以以一個 DSN 字符串的形式提供,示例:connect(dsn='host:MYDB',user='root',password=' ')
當然,不同的數(shù)據(jù)庫接口程序可能有些差異,并非都是嚴格按照規(guī)范實現(xiàn),例如MySQLdb則使用 db 參數(shù)而不是規(guī)范推薦的 database 參數(shù)來表示要訪問的數(shù)據(jù)庫:
MySQLdb連接時可用參數(shù)
- host: 數(shù)據(jù)庫主機名.默認是用本地主機
- user: 數(shù)據(jù)庫登陸名.默認是當前用戶
- passwd: 數(shù)據(jù)庫登陸的秘密.默認為空
- db: 要使用的數(shù)據(jù)庫名.沒有默認值
- port: MySQL服務(wù)使用的TCP端口.默認是3306
- charset: 數(shù)據(jù)庫編碼
psycopg2連接時可用參數(shù):
- dbname – 數(shù)據(jù)庫名稱 (dsn連接模式)
- database – 數(shù)據(jù)庫名稱
- user – 用戶名
- password – 密碼
- host – 服務(wù)器地址 (如果不提供默認連接Unix Socket)
- port – 連接端口 (默認5432)
其中connect對象又有如下方法:
- close():關(guān)閉此connect對象, 關(guān)閉后無法再進行操作,除非再次創(chuàng)建連接
- commit():提交當前事務(wù),如果是支持事務(wù)的數(shù)據(jù)庫執(zhí)行增刪改后沒有commit則數(shù)據(jù)庫默認回滾
- rollback():取消當前事務(wù)
- cursor():創(chuàng)建游標對象
使用cursor創(chuàng)建游標對象
cursor游標對象又有如下屬性和方法:
常用方法:
- close():關(guān)閉此游標對象
- fetchone():得到結(jié)果集的下一行
- fetchmany([size = cursor.arraysize]):得到結(jié)果集的下幾行
- fetchall():得到結(jié)果集中剩下的所有行
- excute(sql[, args]):執(zhí)行一個數(shù)據(jù)庫查詢或命令
- excutemany(sql, args):執(zhí)行多個數(shù)據(jù)庫查詢或命令
常用屬性:
- connection:創(chuàng)建此游標對象的數(shù)據(jù)庫連接
- arraysize:使用fetchmany()方法一次取出多少條記錄,默認為1
- lastrowid:相當于PHP的last_inset_id()
其他方法:
- __iter__():創(chuàng)建一個可迭代對象(可選)
- next():獲取結(jié)果集的下一行(如果支持迭代的話)
- nextset():移到下一個結(jié)果集(如果支持的話)
- callproc(func[,args]):調(diào)用一個存儲過程
- setinputsizes(sizes):設(shè)置輸入最大值(必須有,但具體實現(xiàn)是可選的)
- setoutputsizes(sizes[,col]):設(shè)置大列 fetch 的最大緩沖區(qū)大小
其他屬性:
- description:返回游標活動狀態(tài)(包含7個元素的元組):(name, type_code, display_size, internal_size, precision, scale, null_ok)只有 name 和 type_cose 是必需的
- rowcount:最近一次 execute() 創(chuàng)建或影響的行數(shù)
- messages:游標執(zhí)行后數(shù)據(jù)庫返回的信息元組(可選)
- rownumber:當前結(jié)果集中游標所在行的索引(起始行號為 0)
DB-API只中的錯誤定義
錯誤類的層次關(guān)系:
StandardError |__Warning |__Error |__InterfaceError |__DatabaseError |__DataError |__OperationalError |__IntegrityError |__InternalError |__ProgrammingError |__NotSupportedError
數(shù)據(jù)庫操作示例
代碼如下:
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# *************************************************************
# Filename @ operatemysql.py
# Author @ Huoty
# Create date @ 2015-08-16 10:44:34
# Description @
# *************************************************************
import MySQLdb
# Script starts from here
# 連接數(shù)據(jù)庫
db_conn = MySQLdb.connect(host = 'localhost', user= 'root', passwd = '123456')
# 如果已經(jīng)創(chuàng)建了數(shù)據(jù)庫,可以直接用如下方式連接數(shù)據(jù)庫
#db_conn = MySQLdb.connect(host = "localhost", user = "root",passwd = "123456", db = "testdb")
"""
connect方法常用參數(shù):
host: 數(shù)據(jù)庫主機名.默認是用本地主機
user: 數(shù)據(jù)庫登陸名.默認是當前用戶
passwd: 數(shù)據(jù)庫登陸的秘密.默認為空
db: 要使用的數(shù)據(jù)庫名.沒有默認值
port: MySQL服務(wù)使用的TCP端口.默認是3306
charset: 數(shù)據(jù)庫編碼
"""
# 獲取操作游標
cursor = db_conn.cursor()
# 使用 execute 方法執(zhí)行SQL語句
cursor.execute("SELECT VERSION()")
# 使用 fetchone 方法獲取一條數(shù)據(jù)庫。
dbversion = cursor.fetchone()
print "Database version : %s " % dbversion
# 創(chuàng)建數(shù)據(jù)庫
cursor.execute("create database if not exists dbtest")
# 選擇要操作的數(shù)據(jù)庫
db_conn.select_db('dbtest');
# 創(chuàng)建數(shù)據(jù)表SQL語句
sql = """CREATE TABLE if not exists employee(
first_name CHAR(20) NOT NULL,
last_name CHAR(20),
age INT,
sex CHAR(1),
income FLOAT )"""
try:
cursor.execute(sql)
except Exception, e:
# Exception 是所有異常的基類,這里表示捕獲所有的異常
print "Error to create table:", e
# 插入數(shù)據(jù)
sql = """INSERT INTO employee(first_name,
last_name, age, sex, income)
VALUES ('%s', '%s', %d, '%s', %d)"""
# Sex: Male男, Female女
employees = (
{"first_name": "Mac", "last_name": "Mohan", "age": 20, "sex": "M", "income": 2000},
{"first_name": "Wei", "last_name": "Zhu", "age": 24, "sex": "M", "income": 7500},
{"first_name": "Huoty", "last_name": "Kong", "age": 24, "sex": "M", "income": 8000},
{"first_name": "Esenich", "last_name": "Lu", "age": 22, "sex": "F", "income": 3500},
{"first_name": "Xmin", "last_name": "Yun", "age": 31, "sex": "F", "income": 9500},
{"first_name": "Yxia", "last_name": "Fun", "age": 23, "sex": "M", "income": 3500}
)
try:
# 清空表中數(shù)據(jù)
cursor.execute("delete from employee")
# 執(zhí)行 sql 插入語句
for employee in employees:
cursor.execute(sql % (employee["first_name"], \
employee["last_name"], \
employee["age"], \
employee["sex"], \
employee["income"]))
# 提交到數(shù)據(jù)庫執(zhí)行
db_conn.commit()
# 對于支持事務(wù)的數(shù)據(jù)庫, 在Python數(shù)據(jù)庫編程中,
# 當游標建立之時,就自動開始了一個隱形的數(shù)據(jù)庫事務(wù)。
# 用 commit 方法能夠提交事物
except Exception, e:
# Rollback in case there is any error
print "Error to insert data:", e
#b_conn.rollback()
print "Insert rowcount:", cursor.rowcount
# rowcount 是一個只讀屬性,并返回執(zhí)行execute(方法后影響的行數(shù)。)
# 數(shù)據(jù)庫查詢操作:
# fetchone() 得到結(jié)果集的下一行
# fetchmany([size=cursor.arraysize]) 得到結(jié)果集的下幾行
# fetchall() 返回結(jié)果集中剩下的所有行
try:
# 執(zhí)行 SQL
cursor.execute("select * from employee")
# 獲取一行記錄
rs = cursor.fetchone()
print rs
# 獲取余下記錄中的 2 行記錄
rs = cursor.fetchmany(2)
print rs
# 獲取剩下的所有記錄
ars = cursor.fetchall()
for rs in ars:
print rs
# 可以用 fetchall 獲得所有記錄,然后再遍歷
except Exception, e:
print "Error to select:", e
# 數(shù)據(jù)庫更新操作
sql = "UPDATE employee SET age = age + 1 WHERE sex = '%c'" % ('M')
try:
# 執(zhí)行SQL語句
cursor.execute(sql)
# 提交到數(shù)據(jù)庫執(zhí)行
db_conn.commit()
cursor.execute("select * from employee")
ars = cursor.fetchall()
print "After update: ------"
for rs in ars:
print rs
except Exception, e:
# 發(fā)生錯誤時回滾
print "Error to update:", e
db.rollback()
# 關(guān)閉數(shù)據(jù)庫連接
db_conn.close()
其他參考文檔:
https://www.python.org/dev/peps/pep-0249/
http://wiki.python.org/moin/DatabaseInterfaces
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。
- python使用mysqldb連接數(shù)據(jù)庫操作方法示例詳解
- python連接數(shù)據(jù)庫的方法
- python mysqldb連接數(shù)據(jù)庫
- 跟老齊學(xué)Python之通過Python連接數(shù)據(jù)庫
- Python使用sqlalchemy模塊連接數(shù)據(jù)庫操作示例
- 學(xué)習(xí)python之編寫簡單簡單連接數(shù)據(jù)庫并執(zhí)行查詢操作
- Python 3.x 連接數(shù)據(jù)庫示例(pymysql 方式)
- Python使用Flask-SQLAlchemy連接數(shù)據(jù)庫操作示例
- 解決python3 Pycharm上連接數(shù)據(jù)庫時報錯的問題
- Python與數(shù)據(jù)庫交互:入門指南
相關(guān)文章
python2.7實現(xiàn)爬蟲網(wǎng)頁數(shù)據(jù)
這篇文章主要為大家詳細介紹了python2.7實現(xiàn)爬蟲網(wǎng)頁數(shù)據(jù),具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-05-05
Python數(shù)據(jù)分析之Matplotlib數(shù)據(jù)可視化
這篇文章主要介紹了Python數(shù)據(jù)分析之Matplotlib數(shù)據(jù)可視化,Matplotlib?是?Python?中常用的?2D?繪圖庫,它能輕松地將數(shù)據(jù)進行可視化,作出精美的圖表2022-08-08
OpenCV-Python直方圖均衡化實現(xiàn)圖像去霧
直方圖均衡化可以達到增強圖像顯示效果的目的。最常用的比如去霧。本文就來實現(xiàn)直方圖均衡化實現(xiàn)圖像去霧,感興趣的可以了解一下2021-06-06

