本文最後更新於 1163 天前,其中的信息可能已經有所發展或是發生改變。
將 0001 題生成的 200 個激活碼(或者優惠券)保存到 MySQL 關系型數據庫中。
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("執行sql:"+sql)
cursor.execute(sql)
cursor.connection.commit()
finally:
db.close()
main()
Post Views: 305