Skip to content

Commit 7dc56e7

Browse files
committed
add a solution
1 parent 4ed5930 commit 7dc56e7

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

lenzzz/0002/main.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from random import randint
2+
import MySQLdb
3+
def makeCode(length,number):
4+
code = []
5+
code_set = set(code)
6+
code_map = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
7+
limit = len(code_map)
8+
while(len(code_set)<number):
9+
this_code = ""
10+
for i in range(length):
11+
this_code += code_map[randint(0,limit-1)]
12+
code.append(this_code)
13+
code_set = set(code)
14+
15+
return code_set
16+
17+
def insertDatebase(code):
18+
db = MySQLdb.Connect("localhost","root","","pythonmysql")
19+
cursor = db.cursor()
20+
sql = """INSERT INTO my_practice
21+
VALUES ('%s')""" % code
22+
try:
23+
cursor.execute(sql)
24+
db.commit()
25+
except:
26+
db.rollback()
27+
db.close()
28+
29+
if __name__ == '__main__':
30+
codes = makeCode(20,200)
31+
for code in codes:
32+
insertDatebase(code)

0 commit comments

Comments
 (0)