在工作用有时候需要批量造测试数据;手工造太麻烦了,可以通过 python 批量插入表数据
'''批量插入 sql语句 '''
import pymysql, string ,random,time
def connet_mysql():
try:
db=pymysql.connect(host='192.168.31.103',user=' root ',password='123456',
db='test',port=3306)
except Exception as e:
print('数据库连接失败',e)
return db
def insert_data(id,username,password):
db=connet_mysql()
cursor=db.cursor()
sql _1='insert into user_test(id,user,password)values (%s,%s,%s)'
sql_2='select * from user_test'
params =(id,username,password)
cursor.execute(sql_1,params)
cursor.execute(sql_2)
db.commit()
all=cursor.fetchall()#通过游标,获取查询内容
print(all)
def info():
str_1d=string.digits
str_2a=string.ascii_letters
str_3=str_1d+str_2a
for i in range(501,601):
id=i
username='user'+str(i)
password=''.join(random.sample(str_3,6))
insert_data(id,username,password)
if __name__ == '__main__':
info()