溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Python中第三方庫(kù)封裝mysql數(shù)據(jù)庫(kù)的示例分析

發(fā)布時(shí)間:2021-10-25 16:53:57 來(lái)源:億速云 閱讀:157 作者:柒染 欄目:編程語(yǔ)言

本篇文章為大家展示了Python中第三方庫(kù)封裝mysql數(shù)據(jù)庫(kù)的示例分析,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。

  這次封裝數(shù)據(jù)庫(kù),就算不是為了自動(dòng)化測(cè)試,數(shù)據(jù)庫(kù)操作也必須要會(huì),為什么?因?yàn)樵诤芏鄷r(shí)候,修復(fù)現(xiàn)網(wǎng)用戶數(shù)據(jù)都是通過(guò)腳本實(shí)現(xiàn)的,如果不用其他語(yǔ)言,那么python在這一塊的優(yōu)勢(shì)就十分明顯了。

  '''

  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ù)庫(kù)信息,創(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ù)庫(kù)執(zhí)行方法

  :param sql:執(zhí)行的sql語(yǔ)句,str類型

  :param arg:參數(shù)必須是序列類型:list、str、tuple

  :param is_more:是否查詢顯示更多數(shù)據(jù),通常根據(jù)sql的limit來(lái)確定

  :return:

  '''

  self.mysql_execute(sql, arg)

  if is_more:

  result = self.cursor.fetchall()

  else:

  result = self.cursor.fetchone()

  # 不合適,如果還有sql在循環(huán)執(zhí)行,在這里關(guān)閉,會(huì)造成sql執(zhí)行失敗

  # self.mysql_close()

  return result

  def mysql_execute(self, sql, arg=None):

  '''鄭州人流醫(yī)院哪家好 http://mobile.zhongyuan120.com/

  封裝自己執(zhí)行sql語(yǔ)句的方法

  '''

  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: 返回的是一個(gè)嵌套字典的元組

  '''

  try:

  self.cursor.execute(sql) # arg必須是序列類型

  except Exception as a:

  self.logger.info("執(zhí)行SQL語(yǔ)句出現(xiàn)異常:%s" % a)

  raise a # 直接拋出異常,不在執(zhí)行后面的語(yǔ)句

  else:

  rows = self.cursor.fetchall()

  return rows

  def mysql_getrow(self, sql):

  '''

  返回查詢結(jié)果集

  return:結(jié)果是個(gè)字典

  '''

  try:

  self.cursor.execute(sql) # arg必須是序列類型

  except Exception as a:

  self.logger.info("執(zhí)行SQL語(yǔ)句出現(xiàn)異常:%s" % a)

  raise a

  else:

  row = self.cursor.fetchone()

  return row

  def mysql_close(self):

  '''

  關(guān)閉數(shù)據(jù)庫(kù)連接,關(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中第三方庫(kù)封裝mysql數(shù)據(jù)庫(kù)的示例分析,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI