1 #-*-coding:utf-8-*-2 __author__='Deen' 3 " "4 Title Description:5 Save the 200 activation codes (or coupons) generated by the 0001 questions to the MySQL relational database. 6 " "7 """8 import MySQLdb as MDB9 Ten config = { One ' host ': ' 127.0.0.1 ', A ' Port ': 3306, - ' user ': ' Root ', - ' passwd ': ', the ' db ': ' Student ', - ' charset ': ' UTF8 ' - } - conn = Mdb.connect (**config) + cursor = conn.cursor (cursorclass=mdb.cursors.dictcursor) - + A at cursor.execute (' Set names GBK ') - - - - n=0 - num=list () in - to For line in open ("Student12.txt"): + line=line.decode (' GBK '). Encode (' Utf-8 ') - the n=n+1 * $ if n%6==1:Panax Notoginseng Cursor.execute (' INSERT into student12 (SNO) VALUES (%s) '% (line)) - the """ + A ImportMySQLdb as MDB the +Config = { - 'Host':'127.0.0.1', $ 'Port': 3306, $ 'User':'Root', - 'passwd':"', - 'DB':'ctf_test', the 'CharSet':'UTF8' - }Wuyiconn = Mdb.connect (* *config) thecursor = Conn.cursor (cursorclass=mdb.cursors.DictCursor) -Cursor.execute ('Set names GBK') Wu -Cursor.execute ('CREATE TABLE codes (ID int (5), code char )') AboutWith open ('Poll_codes.txt','R') as FP: $ forLineinchfp.readlines (): -line = Line.strip ('\ n'). Split (':') -Cursor.execute ('INSERT into Codes VALUES (%s,%s)', line) - A + Conn.commit () the cursor.close () -Conn.close ()
Python Practice book A small program for the No. 0002 question every day