import pymysql
conn = pymysql.connect(user=
'root',password=
'password',database=
'test')
cursor = conn.cursor()
cursor.execute(
'''create table if not exists actcode(
id smallint primary key,
code varchar(50) not null);
''')
cursor.execute(
'delete from actcode')
for i,_code
in enumerate(codelist):
cursor.execute(
'insert actcode(id,code) values (%s,"%s")'%(i,_code))
conn.commit()
cursor.close()
conn.close()
import random,string
CHRLIST=string.ascii_letters+string.digits
codelist=[]
def many_code(lens,n):
c=
0
while True:
code=single_code(lens)
if code
in codelist:
c=c
else:
codelist.append(code)
c=c+
1
if c==n:
break
print(len(codelist))
def single_code(lens):
code=
''
for x
in range(lens):
i=random.randint(
0,CHRLIST.__len__()-
1)
code+=CHRLIST[i]
return '-'.join(code[i:i+
4]
for i
in range(
0,len(code),
4))
many_code(
16,
200)
首先,需要安装pymysql,pip3 install pymysql 注意,语句操作后要conn.commit()