支持参数,可以不加参数
-p 存储路径 (默认/root/t100w.sql)
-n 生成多少条数据 (默认 1000001)
#!/usr/bin/env python #-*- coding:utf-8 -*- import optparse import random import time database = 'create database IF NOT EXISTS test; ' table = ''' CREATE table if not EXISTS test.t100w( id INT not null PRIMARY KEY auto_increment COMMENT '主键', k1 char(2) not null default 'a' COMMENT 'k1', k2 varchar(20) not null default 'b' COMMENT 'k2', in_time datetime not null default now() COMMENT '时间' ); ''' sql = 'insert into test.t100w(k1,k2,in_time) values("%s","%s","%s"); ' s = 'qazwsxedcrfvtgbyhnujmikolp' def createinsert(path,maxnum): with open(path,'a') as f: f.write(database+table) for i in range(1,maxnum): k1 = ''.join(random.sample(s,2)) k2 = ''.join(random.sample(s,4)) f.write(sql%(k1,k2,time.strftime('%Y-%m-%d'))) def verify_argv(options): ''' 参数验证 :param options: :param argv: :return: ''' path = option.path if option.path else '/root/t100w.sql' num = int(option.num) if option.num else 1000001 return path,num if __name__ == '__main__': op = optparse.OptionParser() op.add_option('-p','--path',dest="path",help="目标路径") op.add_option('-n', '--num', dest="num", help="生成数量") option,argv = op.parse_args() path,num = verify_argv(option) createinsert(path,num)
使用方法
python s.py -p /data/t1.sql -n 100000
生成后,登录mysql
source /data/t1.sql