亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Python接口測試數(shù)據(jù)庫封裝實現(xiàn)原理

 更新時間:2020年05月09日 10:20:51   作者:全棧測試開發(fā)日記  
這篇文章主要介紹了Python接口測試數(shù)據(jù)庫封裝實現(xiàn)原理,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

引言

  做接口測試的時候,避免不了操作數(shù)據(jù)庫。因為數(shù)據(jù)校驗需要,測試數(shù)據(jù)初始化需要、一些參數(shù)化場景需要等。

  數(shù)據(jù)庫操作框架設(shè)計

  這里主要操作mysql數(shù)據(jù)庫,整體思路:

  封裝實現(xiàn)

  具體代碼實現(xiàn):

import pymysql
import json
 
 
class OperateMysql(object):
  def __init__(self):
    # 數(shù)據(jù)庫初始化連接
    self.connect_interface_testing = pymysql.connect(
      "localhost",
      "root",
      "123456",
      "test",
      charset='utf8mb4',
      cursorclass=pymysql.cursors.DictCursor
    )
 
    # 創(chuàng)建游標操作數(shù)據(jù)庫
    self.cursor_interface_testing = self.connect_interface_testing.cursor()
 
  def select_first_data(self, sql):
    """
    查詢第一條數(shù)據(jù)
    """
    try:
      # 執(zhí)行 sql 語句
      self.cursor_interface_testing.execute(sql)
    except Exception as e:
      print("執(zhí)行sql異常:%s"%e)
    else:
      # 獲取查詢到的第一條數(shù)據(jù)
      first_data = self.cursor_interface_testing.fetchone()
      # print(first_data)
      # 將返回結(jié)果轉(zhuǎn)換成 str 數(shù)據(jù)格式,禁用acsii編碼
      first_data = json.dumps(first_data,ensure_ascii=False)
      # self.connect_interface_testing.close()
      return first_data
 
  def select_all_data(self,sql):
    """
    查詢結(jié)果集
    """
    try:
      self.cursor_interface_testing.execute(sql)
    except Exception as e:
      print("執(zhí)行sql異常:%s"%e)
    else:
      first_data = self.cursor_interface_testing.fetchall()
      first_data = json.dumps(first_data,ensure_ascii=False)
      # self.connect_interface_testing.close()
      return first_data
 
  def del_data(self,sql):
    """
    刪除數(shù)據(jù)
    """
    res = {}
    try:
      # 執(zhí)行SQL語句
      result = self.cursor_interface_testing.execute(sql)
      # print(result)
      if result != 0:
        # 提交修改
        self.connect_interface_testing.commit()
        res = {'刪除成功'}
      else:
        res = {'沒有要刪除的數(shù)據(jù)'}
    except:
      # 發(fā)生錯誤時回滾
      self.connect_interface_testing.rollback()
      res = {'刪除失敗'}
    return res
 
  def update_data(self,sql):
    """
    修改數(shù)據(jù)
    """
    try:
      self.cursor_interface_testing.execute(sql)
      self.connect_interface_testing.commit()
      res = {'更新成功'}
    except Exception as e:
      self.connect_interface_testing.rollback()
      res = {'更新刪除'}
    return res
 
  def insert_data(self,sql,data):
    """
    新增數(shù)據(jù)
    """
 
    try:
      self.cursor_interface_testing.execute(sql,data)
      self.connect_interface_testing.commit()
      res = {data,'新增成功'}
    except Exception as e:
      res = {'新增失敗',e}
    return res
  def conn_close(self):
    # 關(guān)閉數(shù)據(jù)庫
    self.cursor_interface_testing.close()
 
 
if __name__ == "__main__":
  # ()類的實例化
  om = OperateMysql()
 
  # 新增
  data = [{'id': 1, 'name': '測試', 'age': 15}, {'id': 2, 'name': '老王', 'age': 10}, {'id': 3, 'name': '李四', 'age': 20}]
  for i in data:
    i_data = (i['id'],i['name'],i['age'])
    insert_res = om.insert_data(
      """
       INSERT INTO test_student (id,name,age) VALUES (%s,%s,%s)
      """,i_data
    )
    print(insert_res)
 
  # 查詢
  one_data = om.select_first_data(
    """
      SELECT * FROM test_student;
    """
  )
  all_data = om.select_all_data(
    """
    SELECT * FROM test_student;
    """
  )
  print(one_data)
  # all_data字符串類型的list轉(zhuǎn)list
  print("查詢總數(shù)據(jù):%s",len(json.loads(all_data)),"分別是:%s",all_data)
 
  # 修改
  update_data = om.update_data(
    """
    UPDATE test_student SET name = '王五' WHERE id = 1;
    """
  )
  print(update_data)
 
  # 刪除
  del_data = om.del_data(
    """
    DELETE FROM test_student WHERE id in (1,2,3);
    """
  )
  print(del_data)
 
  # 關(guān)閉游標
  om.conn_close()

運行結(jié)果:

為了方便演示,先注釋刪除數(shù)據(jù)的sql,再執(zhí)行程序:

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • opencv 獲取rtsp流媒體視頻的實現(xiàn)方法

    opencv 獲取rtsp流媒體視頻的實現(xiàn)方法

    這篇文章主要介紹了opencv 獲取rtsp流媒體視頻的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-08-08
  • Python中的單行、多行、中文注釋方法

    Python中的單行、多行、中文注釋方法

    今天小編就為大家分享一篇Python中的單行、多行、中文注釋方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-07-07
  • 詳解如何在PyQt5中實現(xiàn)平滑滾動的QScrollArea

    詳解如何在PyQt5中實現(xiàn)平滑滾動的QScrollArea

    Qt 自帶的 QScrollArea 滾動時只能在兩個像素節(jié)點之間跳變,看起來很突兀。所以本文將通過定時器,重寫 wheelEvent() 來實現(xiàn)平滑滾動,需要的可以參考一下
    2023-01-01
  • pytorch快速搭建神經(jīng)網(wǎng)絡(luò)_Sequential操作

    pytorch快速搭建神經(jīng)網(wǎng)絡(luò)_Sequential操作

    這篇文章主要介紹了pytorch快速搭建神經(jīng)網(wǎng)絡(luò)_Sequential操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-06-06
  • Python實現(xiàn)變聲器功能(蘿莉音御姐音)

    Python實現(xiàn)變聲器功能(蘿莉音御姐音)

    這篇文章主要介紹了Python實現(xiàn)變聲器功能(蘿莉音御姐音),本文圖文實例代碼相結(jié)合給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-12-12
  • Python 內(nèi)置函數(shù)速查表一覽

    Python 內(nèi)置函數(shù)速查表一覽

    這篇文章主要介紹了Python 內(nèi)置函數(shù)速查表,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • Django查詢優(yōu)化及ajax編碼格式原理解析

    Django查詢優(yōu)化及ajax編碼格式原理解析

    這篇文章主要介紹了Django查詢優(yōu)化及ajax編碼格式原理解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-03-03
  • 利用Python編寫一個Windows桌面小組件

    利用Python編寫一個Windows桌面小組件

    這篇文章主要為大家詳細介紹了如何讓ChatGPT按要求編寫Python代碼實現(xiàn)一個Windows桌面小組件,文中的示例代碼講解詳細,感興趣的可以了解一下
    2023-06-06
  • Python遞歸時間復雜度

    Python遞歸時間復雜度

    這篇文章主要介紹了Python遞歸時間復雜度,時間復雜度一般認為O(logn),但遞歸算法的時間復雜度本質(zhì)上是要看遞歸的次數(shù),每次遞歸中的操作次數(shù),下面文章詳細介紹,需要的朋友可以參考一下
    2022-03-03
  • python使用pandas處理大數(shù)據(jù)節(jié)省內(nèi)存技巧(推薦)

    python使用pandas處理大數(shù)據(jù)節(jié)省內(nèi)存技巧(推薦)

    這篇文章主要介紹了python使用pandas處理大數(shù)據(jù)節(jié)省內(nèi)存技巧,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-05-05

最新評論