Python show-me-the-code question No. 0002 MySQL operation

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.