Skip to content

Commit 1817ce6

Browse files
committed
0002
1 parent 0f06bc7 commit 1817ce6

File tree

1 file changed

+42
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)