最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

引言

  做接口測試的時候,避免不了操作數(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)建游標(biāo)操作數(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)閉游標(biāo)
  om.conn_close()

運行結(jié)果:

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

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

相關(guān)文章

  • 使用Python的SymPy庫解決數(shù)學(xué)運算問題的方法

    使用Python的SymPy庫解決數(shù)學(xué)運算問題的方法

    這篇文章主要介紹了使用Python的SymPy庫解決數(shù)學(xué)運算問題的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-03-03
  • 用Python的線程來解決生產(chǎn)者消費問題的示例

    用Python的線程來解決生產(chǎn)者消費問題的示例

    這篇文章主要介紹了用Python的線程來解決生產(chǎn)者消費問題的示例,包括對使用線程中容易出現(xiàn)的一些問題給出了相關(guān)解答,需要的朋友可以參考下
    2015-04-04
  • django和flask哪個值得研究學(xué)習(xí)

    django和flask哪個值得研究學(xué)習(xí)

    在本篇文章里小編給大家整理的是一篇關(guān)于django和flask哪個值得研究學(xué)習(xí)內(nèi)容,需要的朋友們可以參考下。
    2020-07-07
  • Python實現(xiàn)向好友發(fā)送微信消息優(yōu)化篇

    Python實現(xiàn)向好友發(fā)送微信消息優(yōu)化篇

    利用python可以實現(xiàn)微信消息發(fā)送功能,怎么實現(xiàn)呢?你肯定會想著很復(fù)雜,但是python的好處就是很多人已經(jīng)把接口打包做好了,只需要調(diào)用即可,今天通過本文給大家分享使用?Python?實現(xiàn)微信消息發(fā)送的思路代碼,一起看看吧
    2022-06-06
  • Python實戰(zhàn)整活之聊天機器人

    Python實戰(zhàn)整活之聊天機器人

    這篇文章主要介紹了Python實戰(zhàn)整活之聊天機器人,文中有非常詳細(xì)的代碼示例,對正在學(xué)習(xí)python的小伙伴們有非常好的幫助,需要的朋友可以參考下
    2021-04-04
  • Python數(shù)字圖像處理基礎(chǔ)直方圖詳解

    Python數(shù)字圖像處理基礎(chǔ)直方圖詳解

    這篇文章主要介紹了Python數(shù)字圖像處理基礎(chǔ)直方圖詳解,本文對Python直方圖的定義、性質(zhì)、應(yīng)用以及Python直方圖的計算作了詳細(xì)的講解,有需要朋友可以借鑒參考下
    2021-09-09
  • python numpy中array與pandas的DataFrame轉(zhuǎn)換方式

    python numpy中array與pandas的DataFrame轉(zhuǎn)換方式

    這篇文章主要介紹了python numpy中array與pandas的DataFrame轉(zhuǎn)換方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • 導(dǎo)入tensorflow時報錯:cannot import name ''abs''的解決

    導(dǎo)入tensorflow時報錯:cannot import name ''abs''的解決

    這篇文章主要介紹了導(dǎo)入tensorflow時報錯:cannot import name 'abs'的解決,文中介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-10-10
  • Python可視化最頻繁使用的10大工具總結(jié)

    Python可視化最頻繁使用的10大工具總結(jié)

    數(shù)據(jù)可視化是數(shù)據(jù)科學(xué)中不可缺少的一部分,下面這篇文章主要給大家介紹了關(guān)于Python可視化最頻繁使用的10大工具,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-03-03
  • python神經(jīng)網(wǎng)絡(luò)使用tensorflow構(gòu)建長短時記憶LSTM

    python神經(jīng)網(wǎng)絡(luò)使用tensorflow構(gòu)建長短時記憶LSTM

    這篇文章主要為大家介紹了python機器學(xué)習(xí)tensorflow構(gòu)建長短時記憶網(wǎng)絡(luò)LSTM,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-05-05

最新評論

鱼台县| 宁津县| 石柱| 河津市| 会昌县| 中阳县| 顺义区| 萨嘎县| 普洱| 宣恩县| 大宁县| 邛崃市| 辽阳县| 邻水| 延庆县| 玉溪市| 新竹市| 扶余县| 上高县| 增城市| 渭南市| 洱源县| 淳安县| 富蕴县| 湖北省| 溆浦县| 游戏| 迁西县| 大城县| 资中县| 闻喜县| 长丰县| 灵宝市| 邯郸县| 濉溪县| 博客| 吉林省| 德州市| 开封市| 尼玛县| 扎鲁特旗|