Python3 Connection mysql--additions and deletions change

Source: Internet
Author: User

Ps:mysqldb only available for python2.x

Python3 does not support MYSQLDB and is replaced by Pymysql

Run the report: Importerror:no module named ' MySQLdb '

ImportPymysql as PMQ#Connect (ip.user,password,dbname)con = Pmq.connect ('localhost','Root','123456','python_test')#Manipulating CursorsCur =con.cursor ()#Build TableCur.execute ("CREATE TABLE IF not EXISTS writers (Id INT PRIMARY KEY auto_increment,name VARCHAR )")#Insert Data OneCur.execute ("INSERT into writers (Name) VALUES (' Jack London ')") Cur.execute ("INSERT into writers (Name) VALUES (' Honore de Balzac ')") Cur.execute ("INSERT into writers VALUES (1, ' Jack London ')") Cur.execute ("INSERT into Writers VALUES (2, ' Honore de Balzac ')")#submit to Database-------PS: Do not write this line cannot be insertedCon.commit ()#inserting data twoSql="INSERT into writers (Name) VALUES (' Jack London2 ')"Try:#Execute SQL statementcur.execute (SQL)#commit to database executionCon.commit ()except:#Rollback If an error occursCon.rollback ()

1 #Enquiry2Cur.execute ('SELECT * FROM writers')3 4Results =Cur.fetchall ()5 6  forRowinchResults:7Id =Row[0]8Name = row[1]9     Print("id=%s,name=%s"% (Id,name))

1 #Update2Id= 13sql ="Update writers set name= ' Updatejackto ' where Id = {0}". Format (Id)4 Try:5 cur.execute (SQL)6 Con.commit ()7 except:8 Con.rollback ()9     Ten #Delete OneId=7 Asql ="Delete from writers where Id = {0}". Format (Id) - Try: - cur.execute (SQL) the Con.commit () - except: -Con.rollback ()

Python3 Connection mysql--additions and deletions change

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.