question No. 0002 : Save the 200 activation codes (or coupons) generated by the 0001 questions to the MySQL relational database.
Steps:
- Install MySQLdb First, install with PIP
- and understand the next mysqldb operation.
- Introducing the library into your code
- Connect the database, write the data, close the connection
0002.MySQL operation. py
#!/usr/bin/env python #coding: utf-8 import Mysqldbimport gennerate_codehost = ' localhost ' USER = ' root ' PASSWORD = ' 000ooo ' PORT = 3306 DB = ' python ' #连接数据库 conn = MySQLdb.connect (host =host, User=user, Passwd=password, Db=db, Port=port) cur = conn.cursor () #生成200组激活码 codelist = gennerate_code.generate ( Span class= "Hljs-number" >200 ) #将生成的激活码插入到表中 for i in xrange (200 ): sql = " INSERT into code VALUES (\ '%s\ ') ' % codelist[i] cur.execute (SQL) Conn.commit () Cur.close () CO Nn.close ()
Where Gennerate_code is the code in the Python show-me-the-code No. 0001 generating the activation code
Python show-me-the-code question No. 0002 MySQL operation