Working with MySQL in Python

Source: Internet
Author: User

Tag:blog   val    Statement    mit   import    url   utf8    query     generate    

Import pymysql# connect to database connect = Pymysql. Connect (    host= ' localhost ',    port=3306,    user= ' root ',    passwd= ' root ',    db= ' Python3 ',    charset= ' UTF8 ') # gets cursor cursor = connect.cursor () # SQL Operation # Add data operation Sql_1 = "INSERT into Url_file (url,file) VALUES (%s,%s)" data = (' AA ', ' BB ') cursor.execute (sql_1, data)  # Generate an Add SQL statement connect.commit ()  # to confirm the permanent execution of the Add # query data operation (only the query is full of cursors, the other 3 operations, To commit with a connection commit) Sql_2 = "SELECT * from Url_file" Cursor.execute (sql_2)  # Generate query SQL statement ret = Cursor.fetchall ()  # Execute query print ("Done") print (Ret) print (type (ret)) # Modify data Operation Sql_3 = "Update url_file set url= ' bbbbbbbbbbbb ' WHERE id = 2" Cursor.execute (sql_3)  # Generate Modify SQL Statement connect.commit ()  # Confirm permanently execute Modify # delete data operation Sql_4 = "Delete from Url_file where ID =12 "R1 = Cursor.execute (sql_4)  # Generates modified SQL statement, temporarily executes r2 = Connect.commit ()  # confirm Permanent Execution Delete print (r1) print (R2)

  

Working with MySQL in Python

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.