在平時(shí)的工作中,經(jīng)常會(huì)遇到造數(shù)據(jù)棘捣,特別是性能測(cè)試的時(shí)候更是需要大量的數(shù)據(jù)辜腺。如果一條條的插入數(shù)據(jù)庫或者一條條的創(chuàng)建數(shù)據(jù),效率未免有點(diǎn)低。
如何快速的造大量的測(cè)試數(shù)據(jù)呢评疗?
在不熟悉存儲(chǔ)過程的情況下测砂,今天給大家介紹一種方法,很簡(jiǎn)單的也很實(shí)用百匆。思路是用python代碼寫一段小程序砌些,生成一定數(shù)量的SQL語句,再把這些SQL語句拷貝黏貼到數(shù)據(jù)庫工具執(zhí)行SQL即可
以銀行表 j_bank加匈,其表結(jié)構(gòu)為
id` ,`org_id` ,`bank_name`,`bank_account_id`,`owner`, `status` 0-禁用 1-啟用',`deleted_flag` 0-未刪除 1-已刪除', `initial_balance`'期初余額', `type` 銀行庫類型 0-本企業(yè)銀行庫,1-對(duì)方銀行庫,2-員工銀行庫', `audit_status` '審核狀態(tài) 0-待提交, 10-已提交(待審核), 11-新增, 12-編輯, 13-開關(guān), 20-審核拒絕. 30-審核通過', `audit_user_id` varchar(32) DEFAULT NULL COMMENT '審核用戶id', `audit_time` datetime DEFAULT NULL COMMENT '審核時(shí)間'
一存璃、以Mysq為例子,sql語句如下:
INSERT INTO `newgwy-journal`.`j_bank`
(`id`, `org_id`, `bank_name`, `bank_account_id`, `owner`, `status`, `deleted_flag`, `initial_balance`, `type`, `audit_status`, `update_user_id`, `update_time`, `audit_user_id`, `audit_time`)
VALUES (181, '62206eae1d8a1d7c6d7f4053', '合作社hz', '56768792345434612', '', 1, 0, 1200000.00, 0, 30, '0e4ad8c888f147f29617ae7ffcbbd942', '2022-03-23 11:32:10', '74a52f7b09a74807a46a0e52b362cba5', '2022-03-23 10:15:11')
二雕拼、通過python腳本批量生成1000條待插入的SQL語句
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2022/3/23 11:10
# @Author : Celeste
# @Site : http://www.reibang.com/u/71ff901eeee2
# @File : cre_data.py
# @公眾號(hào) :軟件測(cè)試開發(fā)修煉
'''
# 利用python生成造數(shù)據(jù)的sql語句保存在txt文件中纵东,再去mysql中執(zhí)行
import random
import time
#構(gòu)造表 t_user_info
class creat_t_blank_info():
start = time.time()
count = 10000 # 造一萬條數(shù)據(jù)
benginID = 185
# 打開文件,并動(dòng)態(tài)生成sql數(shù)據(jù)啥寇,將數(shù)據(jù)存放在文件中
try:
with open('./blank.txt','wb') as f:
length = count+1
for i in range(1,length):
# 定義數(shù)據(jù)篮迎,測(cè)試數(shù)據(jù)
int_i = int(i+benginID)
bank_name = str(random.choice(["建設(shè)銀行","招商銀行","農(nóng)業(yè)銀行","工商銀行"])) # 銀行名
bank_account_id = random.randrange(1000000,10000000000000) # 銀行賬號(hào)
initial_balance = random.randrange(10,100000000) # 銀行余額
audit_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) # 創(chuàng)建時(shí)間
sql = 'INSERT INTO newgwy-journal.j_bank(id, org_id, bank_name, bank_account_id, owner, status, deleted_flag, initial_balance, type, audit_status, audit_user_id, audit_time) VALUES (str_i ,'62206eae1d8a1d7c6d7f4053',"'+bank_name+'" , "'+bank_account_id+'" ,initial_balance,0,30,'74a52f7b09a74807a46a0e52b362cba5', "'+audit_time+'");'
f.write(sql.encode('UTF-8'))
f.write("\n")
print("創(chuàng)建一萬條sql共耗時(shí):",time.time() - start)
except Exception as e:
print(Exception, ":",e)
if __name__ == '__main__':
creat_t_blank_info()
登錄數(shù)據(jù)庫造數(shù)據(jù)
#coding=utf-8
import pymysql
conn = pymysql.connect(“l(fā)ocalhost”,“root”,“root”,“mysql”,charset=‘utf8’)
cursor = conn.cursor()
for i in range(1115381,10000000):
i=str(i)
sql=‘insert into movie_top(id,movieName) VALUES {}’.format(‘(’+‘’‘+i+’‘’+‘,’+‘’‘+‘moke數(shù)據(jù)’+’‘’+‘)’)
print(sql)
cursor.execute(sql)
conn.commit()
conn.close()
造數(shù)據(jù)的方法有很多,可根據(jù)不同的場(chǎng)景去造不同的數(shù)據(jù)示姿;
推薦使用上一篇的造數(shù)據(jù)方法:
python使用faker及pandas造數(shù)據(jù)到Excel表