python读取数据库并把数据写入本地文件

一,介绍

上周用jmeter做性能测试时,接口B传入的参数需要依赖接口A生成的借贷申请ID,接口A运行完需要把生成的借贷申请ID导出来到一个文件,作为参数传给接口B,刚开始的时候,手动去数据库倒,

倒了几次感觉有些麻烦,就写了一段python读取数据库并将读到数据写入到本地文件

二,python读取数据库代码如下:

# -*- coding:utf-8 -*-
import pymysql


def get_loan_number(file):
connect = pymysql.Connect(
host="0.0.0.0",
port=3306,
user="test",
passwd="12345678",
db="test",
charset='utf8'
)
print("写入中,请等待……")
cursor = connect.cursor()
sql = "select id from application where status='SUBMITTING' and contract like 'Performance-%' and "
"loan_org_party='166490194444444444'"
cursor.execute(sql)
number = cursor.fetchall()
fp = open(file, "w")
loan_count = 0
for loanNumber in number:
loan_count += 1
fp.write(loanNumber[0] + " ")
fp.close()
cursor.close()
connect.close()
print("写入完成,共写入%d条数据……" % loan_count)


if __name__ == "__main__":
file = r"C:Users estDesktoploanNUmber.txt"
get_loan_number(file)

运行结果如下:

 

三、写到本地数据如下:





四、jmeter 用CSV Data Set Config读取本地参数文件

 
 
原文地址:https://www.cnblogs.com/yanpan/p/8671489.html