Python operation MySQL

Source: Internet
Author: User

"Using Python for Database pruning"

The code is as follows:

From pymysql Import *

def main ():

# 1. Create a Connection connection

conn = connect (host= ' localhost ', port=3306,database= ' Jing_dong ', user= ' root ', password= ' MySQL ', charset= ' UTF8 ')

# 2. Get Cursor Object

CS1 = Conn.cursor ()

# executes the INSERT statement and returns the number of rows affected: Add a piece of data

#Cursor对象. Execute (Database operations)

# Add Item "hard drive" count to receive the number of rows affected

Count = Cs1.execute (' INSERT into goods_cates (name) VALUES ("hdd") ')

#打印受影响的行数

Print (count)

#增加物品 "Disc"

Count = Cs1.execute (' INSERT into goods_cates (name) VALUES ("disc") ')

Print (count)

# Update data table

# count = Cs1.execute (' Update goods_cates set name= ' mechanical hard drive ' where name= ' hard disk ')

# Delete Items

# count = Cs1.execute (' Delete from goods_cates where id=6 ')

# Before the action is committed, if it has been executed many times before execute, then all commit

Conn.commit ()

# Close Cursor Object

Cs1.close ()

# Close Connection Object

Conn.close ()

if __name__ = = ' __main__ ':

Main ()

Python operation MySQL

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.