直接上代码
# -*- coding:utf-8 -*- import random import MySQLdb # 打开数据库连接 db=MySQLdb.connect(host="127.0.0.1",user="root",passwd="123456",db="cs",charset="utf8") # 使用cursor()方法获取操作游标 cursor = db.cursor() list_Xing=['赵','钱','孙','李','周','吴','郑','王','冯','陈','褚','卫','蒋','沈','韩','杨','张','李'] list_Ming=['豫','章','故','郡','洪','都','新','府','星','分','翼','轸','地','接','衡','庐','襟','三','江','','而','带','五','湖','控','蛮','荆','而','引','瓯','越','物','华','天','宝','龙','光','射','牛','斗','之','墟','人','杰','地','灵','徐','孺','饯','子'] name=random.choice(list_Xing)+random.choice(list_Ming)+random.choice(list_Ming) sage=random.randint(16,30) print name sql1="insert into student values(null,'{0}',18,1,'北京',175,2);".format(name) sql2='''insert into student values(null,'%s',18,1,'北京',175,2);'''%name # print sql1 print sql2 # cursor.execute(sql1) cursor.execute(sql2) db.commit()