快速生成10000条数据,并插入数据库
快速生成10000条数据,并插入数据库
想要快速生成10000条数据,便捷的方式就是写代码生成,这里选择的是python
步骤1:先在python里面编写代码,快速生成10000条sql语句
a:代表往文件里追加数据信息,如果文件里有数据,则在数据后面追加,如果无数据,则在第一行开始写入
uuid:用于随机生成32位随机数
import uuid
class Adata:
def data(self):
# a代表文件追加
with open('b.txt', 'a', encoding='utf-8') as f:
# 编写sql,追加进b.txt文件里
insert_sql = "insert into t_mat_material_receive(id,warehousing_code," \
"project_id,project_name,area_code,area_name,sub_project_code,sub_project_name,specs," \
"unit,nums,stock_nums,material_name,price,unit_tax,amount,tax,total_amount,unit_weight," \
"total_weight," \
"org_id,dept_id,project_team_id,create_user_id,create_time,del_flag,province_code,city_code) " \
"values "
f.write(insert_sql + "\n")
with open('b.txt', 'a', encoding='utf-8') as f:
for i in range(1):
# 随机生成一个随机数
uid = str(uuid.uuid4())
# 将随机数生成带有的-进行拆分后,再连接在一起
id = ''.join(uid.split('-'))
# print(len(id))
#追加的sql内容
append_sql = "('%s', 'TX-23-04-入-051', " \
"'42761bda256bb7d7bd4c5772aea9ef11', '云南红河项目', '云南省-红河州', '2424645765GG'," \
"'云南红河项目001', '∠80×7×1700', '根', 16, 16, '角钢横担', 1.08716814, 0, 231.84, 4347, " \
"8479.53, 1.08716814, 14.49, '68ecaf3c40ce198abe8d21b967c18c3c'," \
"'1ddc5a1e499a11781e12a80458cbc026', 'ab3d0fd152ceccd2a6bb26545f3a2d3b', " \
"'9341208f2561785d93f951013e4936fa', '2023-04-12 16:57:03', 0, '530000', '532500'), " % id
f.write(append_sql + "\n")
if __name__ == '__main__':
Adata().data()
2.将在文件b.txt生成的sql语句,复制到数据库下进行运行即可。
扩展mysql insert(多条)语句
效率低:insert into 表名(name1,name2...) values(value1,value2......);
效率高:insert into 表名(name1,name2) values(vale1,value2.....),(value1.1,value1.2),().....;