Python3 using Pymysql

Source: Internet
Author: User

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

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.