Python批量向mysql插入数据

由于测试中需要在数据库插入大量数据,进行页面展示的性能测试,以及下载功能的性能测试,所以这里想到使用Python造数据

代码如下

import pymysql

'''连接数据库,包括数据库ip、端口、用户名、密码、数据库'''
db = pymysql.connect(host='xx.xx.x.xx', user='xxx', password='cXX23456', port=xxx,
                     db='mv_flamionxx')
'''创建游标'''
cursor = db.cursor()
i = 1
id1 = 200000
while i <= 10:
    sql = "INSERT INTO `mv_flame_recognition`.`flame_collection_verify`(`id`, `collection_id`, `converter_code`, `converter_num`, `is_verify`, `is_error`, `splash_type`, `splash_start_time`, `splash_end_time`, `gmt_create`, `gmt_modified`, `is_deleted`) VALUES (" + str(
        id1) + ", 43, 'CVT01', 'LC039', b'0', b'1', 4, '2021-06-23 18:43:58', '2021-06-23 18:43:59', '2021-06-23 18:49:58', '2021-06-23 18:49:58', b'0')"
    # values=(i,i)
    cursor.execute(sql)
    '''提交数据,必须提交,不然数据不会保存'''
    db.commit()
    id1 = id1 + 1
    i = i + 1
    print(id1)
    print(i)

print("已经插入完成")
cursor.close()
db.close()

注意:
1)往数据库表插数时,有些字段可能不是char或者text格式的而是int时,会报错TypeError:must be str,not int。要在python里转成str
2)插入的sql语句,值是变量,需要用"++"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值