python--Operation MySQL

Source: Internet
Author: User

steps to operate MySQL :

1. Import MySQL Module

2. Create a connection

3. Creating Cursors

4, perform Select/update/delete/insert and other operations (except select other operations need commit)

5. Close the cursor, close the connection

Import Pymysql

conn = Pymysql.connect (Ip,port,user,password,db,charset,autocommit)

cur = conn.cursor ()

sql = "SELECT * FROM tbl_cpy"

Cur.execute (SQL)

Cur.fetchall ()

Cur.close ()

Conn.close

Cursor:

The cursor type can be either a normal type or a dictionary type.

The dictionary type cursor query results are returned as dictionary types. Cur = conn.cursor (cursor = pymysql.cursors.DictCursor)

A generic type of cursor query results returns a tuple type. Cur.conn.cursor ()

If you are importing results into an Excel table, it is recommended that you use a normal cursor because the query result is a two-bit array with subscript for easy data fetching. (Dictionary unordered)

To view the field names of the query results:

Cur.description ()

Get Data

Cur.fetchall () Get all data

Cur.fetchone () Get one piece of data at a time

Cur.fetchmany (num) Gets the data for the specified number of bars

python--Operation MySQL

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.