-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
ActivationCode_MySql.py
42 lines (37 loc) · 1.24 KB
/
ActivationCode_MySql.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#-*- coding:utf-8 -*-
import uuid
import pymysql
def generateActivationCode(num):
codeList = []
for i in range(num):
code = str(uuid.uuid4()).replace('-','').upper()
while code in codeList:
code = str(uuid.uuid4()).replace('-','').upper()
codeList.append(code)
return codeList
def storeInMysql(codelist):
try:
conn = pymysql.connect(host='127.0.0.1',user='root',passwd='root',db='mysql')
cur = conn.cursor()
except BaseException as e:
print(e)
else:
try:
cur.execute('CREATE DATABASE IF NOT EXISTS activation_code')
cur.execute('USE activation_code')
cur.execute('''CREATE TABLE IF NOT EXISTS code(
id INT NOT NULL AUTO_INCREMENT,
code VARCHAR(32) NOT NULL,
PRIMARY KEY(id)
)''')
for code in codelist:
cur.execute('INSERT INTO code(code) VALUES(%s)',(code))
cur.connection.commit()
except BaseException as e:
print(e)
finally:
cur.close()
conn.close()
if __name__ == '__main__':
storeInMysql(generateActivationCode(200))
print('OK!')