python生成随机数据插入mysql

import random as r
import pymysql 
first=('','','','','','','','','','','','','','','','','','','')
middle=('','','','','','','','','')
last=('','','','','','','','','','','','','','')
name=[]
passwd1=('1234','5678','147','258')

for i in range(101):   
    name1=r.choice(first)+r.choice(middle)+r.choice(last) #末尾有空格的名字
    name2=name1.rstrip()  #去掉末尾空格后的名字
    if name2 not in name: #名字存入列表中,且没有重名
        name.append(name2)
                
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='123',db='test1')
cur = conn.cursor() 

for i in range(len(name)):      #插入数据
    passwd=r.choice(passwd1)    #在密码列表中随机取一个
    cur.execute("insert into a2(name,passwd) values(%s,%s)",(name[i],passwd))#注意用法

cur.execute('select * from a2') #查询数据
for s in cur.fetchall():
    print(s)
    
conn.commit()        
cur.close()
conn.close()






       
原文地址:https://www.cnblogs.com/jmlovepython/p/7388632.html