# -*- coding:utf-8 -*-

'''
@project: ApiAutoTest
@author: Jimmy
@file: mysql_util.py
@ide: PyCharm Community Edition
@time: 2018-12-22 10:30
@blog: https://www.cnblogs.com/gotesting/ ''' '''
1. 连接数据库
2. 查询
3. 建立游标
4. 执行
'''
import pymysql
import traceback
from Common.read_config import ReadConfig class MysqlUtil: # 读取配置文件中的数据库配置信息,连接数据库
def __init__(self):
read_config = ReadConfig()
host = read_config.get_config_str('mysql','host')
port = read_config.get_config_int('mysql','port')
user = read_config.get_config_str('mysql','user')
password = read_config.get_config_str('mysql','pwd')
try:
self.mysql = pymysql.connect(host=host,port=port,user=user,password=password,cursorclass=pymysql.cursors.DictCursor)
except Exception as e:
print('mysql connect failed , please check')
raise e # 查询sql数据,并返回
def fetch_one(self,sql):
db = self.mysql
cursor = db.cursor() # 建立游标
try:
cursor.execute(sql) # 执行sql语句
result = cursor.fetchone() # 返回一条数据
except:
traceback.print_exc() # 输出异常信息
db.rollback()
return result def fetch_all(self,sql):
db = self.mysql
cursor = db.cursor()
try:
cursor.execute(sql)
result = cursor.fetchall()
except:
traceback.print_exc()
return result

python - 接口自动化测试 - MysqlUtil - 数据库操作封装的更多相关文章

  1. python - 接口自动化测试 - GetLog - 日志类封装

    # -*- coding:utf-8 -*- ''' @project: ApiAutoTest @author: Jimmy @file: get_logger.py @ide: PyCharm C ...

  2. python - 接口自动化测试 - HttpRequest - 接口测试类封装

    # -*- coding:utf-8 -*- ''' @project: ApiAutoTest @author: Jimmy @file: http_request.py @ide: PyCharm ...

  3. python - 接口自动化测试 - ReadConfig - 读取配置文件封装

    # -*- coding:utf-8 -*- ''' @project: ApiAutoTest @author: Jimmy @file: read_config.py @ide: PyCharm ...

  4. python接口自动化测试二十七:密码MD5加密 ''' MD5加密 ''' # 由于MD5模块在python3中被移除 # 在python3中使用hashlib模块进行md5操作 import hashlib # 待加密信息 str = 'asdas89799,.//plrmf' # 创建md5对象 hl = hashlib.md5() # Tips # 此处必须声明encode # 若写法为

    python接口自动化测试二十七:密码MD5加密   ''' MD5加密 '''# 由于MD5模块在python3中被移除# 在python3中使用hashlib模块进行md5操作import has ...

  5. Python接口自动化测试框架实战 从设计到开发

    第1章 课程介绍(不要错过)本章主要讲解课程的详细安排.课程学习要求.课程面向用户等,让大家很直观的对课程有整体认知! 第2章 接口测试工具Fiddler的运用本章重点讲解如何抓app\web的htt ...

  6. 基于Python接口自动化测试框架+数据与代码分离(进阶篇)附源码

    引言 在上一篇<基于Python接口自动化测试框架(初级篇)附源码>讲过了接口自动化测试框架的搭建,最核心的模块功能就是测试数据库初始化,再来看看之前的框架结构: 可以看出testcase ...

  7. python接口自动化测试七:获取登录的Cookies

    python接口自动化测试七:获取登录的Cookies,并关联到下一个请求   获取登录的cookies:loginCookies = r.cookies 把获取到的cookies传入请求:cooki ...

  8. python接口自动化测试二十八:连接SQL sever操作

    1.中文乱码问题: (1).文件头加上# -*- coding:utf-8 -*- 或者 #coding=utf8 (2).pymssql.connect连接串中charset是要跟你数据库的编码一样 ...

  9. C# .NET更智能的数据库操作封装项目

    前面两篇文章介绍了框架的思路及里面大概的实现过程,那时候忘记上传项目,就补发一下.顺便介绍下框架使用方式,并分析下框架使用的优缺点. 先发一下前两章的链接 篇一:http://www.cnblogs. ...

随机推荐

  1. JS案例练习 — 给div添加样式选择功能

    附加效果图: CSS内容: <style> ; padding:0px} li{list-style:none} body{font:24px 'Microsoft YaHei'; col ...

  2. nodejs请求中获取参数值的方法

    req.params.xxxxx 从path中的变量 req.query.xxxxx 从get中的?xxxx=中 req.body.xxxxx 从post中的变量

  3. Sqlserver列出所有数据库名,表名,字段名【转】

    1.获取所有数据库名: SELECT Name FROM Master..SysDatabases ORDER BY Name 注意: 表Master与SysDatabases之间有两个点 2.获取所 ...

  4. int _tmain(int argc, _TCHAR* argv[])

    int _tmain(int argc, _TCHAR* argv[]){ int i; for (i = 0; i<argc; i++) cout<<argv[i]<< ...

  5. 创建1M-1T的虚拟磁盘(内存盘)——使用破解版 Primo Ramdisk Server Edition v5.6.0

    破解版 Primo Ramdisk Server Edition v5.6.0下载: https://pan.lanzou.com/i0sgcne 步骤: 下载并解压后安装“Primo.Ramdisk ...

  6. JavaScript: apply , call 方法

    我在一开始看到javascript的函数apply和call时,非常的模糊,看也看不懂,最近在网上看到一些文章对apply方法和call的一些示例,总算是看的有点眉目了,在这里我做如下笔记,希望和大家 ...

  7. python_53_函数补充

    def test1(x,y=2): print(x,y) test1(1) test1(1,3) test1(1,y=4) #默认参数特点:调用函数的时候,默认参数非必须传递,默认参数放在后边 #用途 ...

  8. MacOS内核调试环境搭建

    http://ddeville.me/2015/08/using-the-vmware-fusion-gdb-stub-for-kernel-debugging-with-lldb http://dd ...

  9. 题解 P1280 【尼克的任务】

    传送门 f[i]表示i~n的最长空闲时间: 如果当前无任务就休息一秒(f[i]=f[i+1]+1): 否则f[i]=max(f[i],f[i+当前工作时间]); 用结构体来记录,我们对于每一个时刻开一 ...

  10. 拷贝时间测试=cudamelloc+cudahostalloc

    /* * Copyright 1993-2010 NVIDIA Corporation. All rights reserved. * * NVIDIA Corporation and its lic ...