forked from Show-Me-the-Code/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0002.py
More file actions
46 lines (40 loc) · 1.04 KB
/
0002.py
File metadata and controls
46 lines (40 loc) · 1.04 KB
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
43
44
45
46
# coding = utf-8
__author__ = 'Forec'
# 保随机码至MySQL
import pymysql
import random
def make_number( num, length ):
letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
dic = set()
i = 0
while i < num:
str = ''
for j in range(length):
str += random.choice(letters)
if str not in dic:
dic |= {str}
i += 1
else:
continue
return dic
def save( dic ):
connect = pymysql.connect(
host = '127.0.0.1',
port = 3306,
user = 'root',
passwd = 'VKDARK'
)
cur = connect.cursor()
try:
__create = 'create database if not exists test'
cur.execute(__create)
except:
print('database create error')
connect.select_db('test')
__create_table = 'create table if not exists codes(code char(64))'
cur.execute(__create_table)
cur.executemany('insert into codes values(%s)',dic)
connect.commit()
cur.close()
connect.close()
save(make_number(200,10))