File tree Expand file tree Collapse file tree 2 files changed +45
-0
lines changed Expand file tree Collapse file tree 2 files changed +45
-0
lines changed Original file line number Diff line number Diff line change 1+ from  random  import  Random 
2+ 
3+ 
4+ def  generater (random_length , random_num = 1 ):
5+     activation_code  =  "" 
6+     chars  =  "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789" 
7+     random  =  Random ()
8+     for  i  in  range (random_num ):
9+         for  t  in  range (random_length ):
10+             activation_code  +=  chars [random .randint (0 , len (chars )- 1 )]
11+         return (activation_code )
12+         activation_code  =  "" 
13+ 
14+ if  __name__  ==  '__main__' :
15+     generater (random_length = 20 )
Original file line number Diff line number Diff line change 1+ # 第 0002 题:将 0001 题生成的 200 个激活码(或者优惠券)保存到 MySQL 关系型数据库中。 
2+ import  pymysql 
3+ 
4+ from  generater  import  generater 
5+ 
6+ 
7+ def  generaterToMySql (add_num , code_length ):
8+     db  =  pymysql .connect (
9+         host = "localhost" ,
10+         db = "py3homework" ,
11+         user = "root" ,
12+         password = "root" ,
13+     )
14+ 
15+     cursor  =  db .cursor ()
16+     cursor .execute ("DROP TABLE IF EXISTS ACODES" )
17+     sql_Creat  =  """ CREATE TABLE ACODES ( 
18+                     ID int NOT NULL AUTO_INCREMENT, 
19+                     CODES varchar(200), 
20+                     PRIMARY KEY (ID) ) 
21+                 """ 
22+     cursor .execute (sql_Creat )
23+     for  i  in  range (add_num ):
24+         sql  =  " INSERT INTO ACODES(CODES) VALUES ('{0}') " .format (generater (random_length = code_length ))
25+         cursor .execute (sql )
26+         db .commit ()
27+     db .close ()
28+ 
29+ if  __name__  ==  '__main__' :
30+     generaterToMySql (200 , 20 )
    
 
   
 
     
   
   
          
     
  
    
     
 
    
      
     
 
     
    You can’t perform that action at this time.
  
 
    
  
     
    
      
        
     
 
       
      
     
   
 
    
    
  
 
  
 
     
    
0 commit comments