Python's MySQL learning-data manipulation

Source: Internet
Author: User

1. Increase

1 ImportPymysql as PS2 3 #Open a database connection4db = Ps.connect (host='localhost', user='Root', password='123456', database='Test', charset='UTF8')5 6 #Create a Cursor object7Cur =db.cursor ()8 9sql ="INSERT into USER (name,sex) VALUES (%s, $s)"Ten  OneCur.execute (SQL, ('Amy' , ' women ')) A  - Print("OK") -  the #Commit the transaction (this must be actively committed, otherwise in the database operation increment, delete the result does not change) - Db.commit () -  - #Close Cursors + cur.close () -  + #Close the database ADb.close ()

2. Delete

1 ImportPymysql as PS2 3 #Open a database connection4db = Ps.connect (host='localhost', user='Root', password='123456', database='Test', charset='UTF8')5 6 #Create a Cursor object7Cur =db.cursor ()8 9Cur.execute ('DELETE from USER WHERE name= "Small Fang"')Ten  OneA = Cur.execute ('SELECT * from USER')#The return value is a value of type int A  - #Submit - Db.commit () the  - #view the number of records - Print(a) -  + #Close Cursors - cur.close () +  A #Close the database atDb.close ()

Python's MySQL learning-data manipulation

Related Article

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.