import random
import string
import pymysql
forSelect = string.ascii_letters + string.digits
def generate_code(count,length):
for x in range(count):
Re = ''
for y in range(length):
Re += random.choice(forSelect)
yield Re
def save_code():
conn = pymysql.connect(user='root',passwd='123456', db='python')
cursor = conn.cursor()
codes = generate_code(200,20)
for code in codes:
cursor.execute('INSERT INTO test VALUES("%s")'% code)
conn.commit()
cursor.close()
if __name__ =='__main__':
save_code()
.connect()方法中的psswd和db这两个参数要注意一下