本篇文章為大家展示了Python中第三方庫封裝mysql數(shù)據(jù)庫的示例分析,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。
這次封裝數(shù)據(jù)庫,就算不是為了自動化測試,數(shù)據(jù)庫操作也必須要會,為什么?因為在很多時候,修復(fù)現(xiàn)網(wǎng)用戶數(shù)據(jù)都是通過腳本實現(xiàn)的,如果不用其他語言,那么python在這一塊的優(yōu)勢就十分明顯了。
'''
Created on 2019年10月10日
@author: qguan
'''
import pymysql
from utils.HandleLogging import log as logging
mysql_info = {"host": '127.0.0.1',
"port": 3306,
"user": 'root',
"passwd": 'root',
"db": 'mysql',
"charset": 'utf8'}
class HandleMySQL(object):
'''
classdocs
'''
def __init__(self):
'''
初始化mysql數(shù)據(jù)庫信息,創(chuàng)建連接游標(biāo)
'''
self.db_info = mysql_info
self.logger = logging
try:
self.conn = pymysql.connect(host=self.db_info['host'],
port=self.db_info['port'],
user=self.db_info['user'],
passwd=self.db_info['passwd'],
db=self.db_info['db'],
charset=self.db_info['charset'],
cursorclass=pymysql.cursors.DictCursor)
self.logger.info("success!")
except Exception as a:
self.logger.info("failture:%s" % a)
raise a
else:
self.logger.info("create cursor!")
self.cursor = self.conn.cursor()
def __call__(self, sql, arg=None, is_more=False):
'''
調(diào)用數(shù)據(jù)庫執(zhí)行方法
:param sql:執(zhí)行的sql語句,str類型
:param arg:參數(shù)必須是序列類型:list、str、tuple
:param is_more:是否查詢顯示更多數(shù)據(jù),通常根據(jù)sql的limit來確定
:return:
'''
self.mysql_execute(sql, arg)
if is_more:
result = self.cursor.fetchall()
else:
result = self.cursor.fetchone()
# 不合適,如果還有sql在循環(huán)執(zhí)行,在這里關(guān)閉,會造成sql執(zhí)行失敗
# self.mysql_close()
return result
def mysql_execute(self, sql, arg=None):
'''鄭州人流醫(yī)院哪家好 http://mobile.zhongyuan120.com/
封裝自己執(zhí)行sql語句的方法
'''
try:
self.cursor.execute(sql)
except Exception as a:
self.conn.rollback() # sql回滾處理
self.logger.info("回滾sql,異常如下:{}".format(a))
raise a
else:
self.conn.commit() # sql提交
def mysql_getrows(self, sql):
'''
返回查詢結(jié)果集
:return: 返回的是一個嵌套字典的元組
'''
try:
self.cursor.execute(sql) # arg必須是序列類型
except Exception as a:
self.logger.info("執(zhí)行SQL語句出現(xiàn)異常:%s" % a)
raise a # 直接拋出異常,不在執(zhí)行后面的語句
else:
rows = self.cursor.fetchall()
return rows
def mysql_getrow(self, sql):
'''
返回查詢結(jié)果集
return:結(jié)果是個字典
'''
try:
self.cursor.execute(sql) # arg必須是序列類型
except Exception as a:
self.logger.info("執(zhí)行SQL語句出現(xiàn)異常:%s" % a)
raise a
else:
row = self.cursor.fetchone()
return row
def mysql_close(self):
'''
關(guān)閉數(shù)據(jù)庫連接,關(guān)閉游標(biāo)
'''
try:
self.conn.close()
self.cursor.close()
self.logger.info("正常關(guān)閉mysql連接!")
except Exception as a:
self.logger.info("關(guān)閉mysql連接池失敗,異常如下:{}".format(a))
raise a
if __name__ == '__main__':
mq = HandleMySQL()
sql = 'SELECT *from user limit 3'
# print(mq(sql, is_more=True))
mq.mysql_close()
上述內(nèi)容就是Python中第三方庫封裝mysql數(shù)據(jù)庫的示例分析,你們學(xué)到知識或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識儲備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。