This paper is finally updated at 1163 Days ago, , The information may have developed or changed .
take 0001 Question generated 200 Activation codes ( Or coupons ) Save to MySQL In relational database .
import string
import random
import pymysql
db=pymysql.connect(host="localhost",
user="root",
password="Yuyy0800",
db="big_database",
charset="utf8")
def getKeys():
list=[]
str=string.ascii_uppercase
for i in range(200):
key = ""
for j in range(16):
key+=random.choice(str)
if (j+1)%4==0 and j !=15:
key+="-"
if not key in list:
list.append(key)
print(key)
else:
i-=1
return list
def main():
list=getKeys()
try:
with db.cursor() as cursor:
for i in range(200):
sql="insert into pythoneveryday0002keys (keyName) values ('"+\
list[i]+"');"
print(" perform sql:"+sql)
cursor.execute(sql)
cursor.connection.commit()
finally:
db.close()
main()
Post Views: 305