這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)碛嘘P(guān)Python基于pyMySQL的數(shù)據(jù)庫操作類的安裝運(yùn)行過程,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
創(chuàng)新互聯(lián)建站是專業(yè)的鹽池網(wǎng)站建設(shè)公司,鹽池接單;提供網(wǎng)站建設(shè)、成都網(wǎng)站建設(shè),網(wǎng)頁設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行鹽池網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來合作!
一 簡介
Python和MySQL交互的模塊有 MySQLdb 和 PyMySQL(pymysql),MySQLdb是基于C 語言編寫的,而且Python3 不在支持MySQLdb 。PyMySQL是一個(gè)純Python寫的MySQL客戶端,它的目標(biāo)是替代MySQLdb,可以在CPython、PyPy、IronPython和Jython環(huán)境下運(yùn)行,PyMySQL在MIT許可下發(fā)布。
在開發(fā)基于Python語言的項(xiàng)目中,為了以后系統(tǒng)能兼容Python3,我們使用了PyMySQL替換了MySQLdb。下面我們來熟悉一下pymysql的使用。
二 安裝方式
pymsql的源碼 https://github.com/PyMySQL/PyMySQL ,目前還在持續(xù)更新。
安裝要求:
Python -- one of the following:
CPython >= 2.6 or >= 3.3
PyPy >= 4.0
IronPython 2.7
MySQL Server -- one of the following:
MySQL >= 4.1 (tested with only 5.5~)
MariaDB >= 5.1
安裝
pip install PyMySQL
三 基于pymysql的數(shù)據(jù)庫交互
#!/usr/bin/env python
# encoding: utf-8
"""
author: yangyi@youzan
time: 2015/6/8 上午11:34
func: 基于pymysql的數(shù)據(jù)庫交互類,支持事務(wù)提交和回滾,返回結(jié)果記錄行數(shù),和insert的最新id
"""
import pymysql
from warnings import filterwarnings
filterwarnings('ignore', category=pymysql.Warning)
CONNECT_TIMEOUT = 100
IP = 'localhost'
PORT = 3306
USER = 'root'
PASSSWORD = ''
class QueryException(Exception):
"""
"""
class ConnectionException(Exception):
"""
"""
class MySQL_Utils():
def __init__(
self, ip=IP, port=PORT, user=USER, password=PASSSWORD,
connect_timeout=CONNECT_TIMEOUT, remote=False, socket='', dbname='test'):
self.__conn = None
self.__cursor = None
self.lastrowid = None
self.connect_timeout = connect_timeout
self.ip = ip
self.port = port
self.user = user
self.password = password
self.mysocket = socket
self.remote = remote
self.db = dbname
self.rows_affected = 0
def __init_conn(self):
try:
conn = pymysql.connect(
host=self.ip,
port=int(self.port),
user=self.user,
db=self.db,
connect_timeout=self.connect_timeout,
charset='utf8', unix_socket=self.mysocket)
except pymysql.Error as e:
raise ConnectionException(e)
self.__conn = conn
def __init_cursor(self):
if self.__conn:
self.__cursor = self.__conn.cursor(pymysql.cursors.DictCursor)
def close(self):
if self.__conn:
self.__conn.close()
self.__conn = None
#專門處理select 語句
def exec_sql(self, sql, args=None):
try:
if self.__conn is None:
self.__init_conn()
self.__init_cursor()
self.__conn.autocommit = True
self.__cursor.execute(sql, args)
self.rows_affected = self.__cursor.rowcount
results = self.__cursor.fetchall()
return results
except pymysql.Error as e:
raise pymysql.Error(e)
finally:
if self.__conn:
self.close()
# 專門處理dml語句 delete,updete,insert
def exec_txsql(self, sql, args=None):
try:
if self.__conn is None:
self.__init_conn()
self.__init_cursor()
if self.__cursor is None:
self.__init_cursor()
self.rows_affected=self.__cursor.execute(sql, args)
self.lastrowid = self.__cursor.lastrowid
return self.rows_affected
except pymysql.Error as e:
raise pymysql.Error(e)
finally:
if self.__cursor:
self.__cursor.close()
self.__cursor = None
# 提交
def commit(self):
try:
if self.__conn:
self.__conn.commit()
except pymysql.Error as e:
raise pymysql.Error(e)
finally:
if self.__conn:
self.close()
#回滾操作
def rollback(self):
try:
if self.__conn:
self.__conn.rollback()
except pymysql.Error as e:
raise pymysql.Error(e)
finally:
if self.__conn:
self.close()
# 適用于需要獲取插入記錄的主鍵自增id
def get_lastrowid(self):
return self.lastrowid
#獲取dml操作影響的行數(shù)
def get_affectrows(self):
return self.rows_affected
#MySQL_Utils初始化的實(shí)例銷毀之后,自動(dòng)提交
def __del__(self):
self.commit()
上述就是小編為大家分享的Python基于pymysql的數(shù)據(jù)庫操作類的安裝運(yùn)行過程了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。