1 #!/usr/bin/env Python32 #coding = Utf-83 4 ImportRandom5 ImportPymysql6 7 8 #Connect Database Functions9 defconndb (data):Tenconn = Pymysql.connect (host='localhost', user='Root', passwd=' #######', db='Test',) #数据库 OneCur =conn.cursor () #游标 ACur.execute ('CREATE database if not exists test;') #执语句行 -Cur.execute ('CREATE table if not EXISTS test1 (ID int. not NULL, num VARCHAR (+));') - forIinchRange (len (data)): theCur.execute ('INSERT INTO test1 (id,num) VALUES ("{0}", "{1}");'. Format (I,data[i])) #{0} {1} to differentiate from SQL statements - cur.close () #关游标 - Conn.commit () - conn.close () #关数据库 + - #Generate Activation Code + defMake_number (num,length): ALstr ='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' atA = [] -CNT =0 - whileCNT <Num: -A_str ="' - forJinchRange (length): -A_str + =Random.choice (LSTR) in ifA_str not inchA: - a.append (A_STR) toCNT +=1 + returna - the * if __name__=="__main__": $Nums = Make_number (100,20)Panax Notoginseng Print(nums) -CONNDB (Nums)
Error handling:
Pymysql.err.InternalError: (1054, "Unknown column ' k0f3hnczurxia4wmek6a ' in ' Field List ')
{0} {1} is to be distinguished from the SQL statement, so double quotation marks are used in the statement, and other formatting strings (d%,s%, etc. are also applied)
Python3 using Pymysql