python-快速批量插入数据

   平常测试过程中,我们经常需要造测试数据,那么如何能够快速的造批量数据呢?

   一般有三种方法:1:meter或者loadrunner编写脚本发交易造数据  2:写存储过程造数据  3:程序Python写个脚本执行

   这三种方法平常工作中我都有用到,今天就来说一下用Python造数据,个人觉得还是挺方便的

一:首先需要装一个工具类 :cmd下直接执行命令  pip install pymysql   ,安装完成有提示

二:创建个脚本,就可以轻松批量插入数据啦

注意:其中的变量自己可以定义

代码如下 

import pymysql
def connect_mysql():
    conn = pymysql.connect(host='数据库地址', port=数据库端口, user='用户名', passwd='密码',db='数据库实例名')
    cursor = conn.cursor()# 创建游标
    #exce_sql='update orders set order_status="3" where out_order_no="A0004012A0004012A0004012A0004012"'
    exce_sql = "INSERT INTO test (id,serial,name) VALUES (%s,%s,%s)"
    for i in range(100,102):
        print(exce_sql)
        cursor.execute(exce_sql,("202001168888000"+str(i),"202001168888000"+str(i),"name"))    #传值
        #result = cursor.fetchone()
        conn.commit() 
    cursor.close()
    conn.close()
connect_mysql()  

三:执行结果

原文地址:https://www.cnblogs.com/qiaoli0726/p/12204947.html