Python Operation MySQL

Source: Internet
Author: User
Tags mysql insert

First, MySQL basic operation

1. Create test Database

2. Create a new table

3. Inserting data into the MSG table

4, the data in the table to increase the deletion and search

Second, python operation MySQL

Import MySQLdb

conn = MySQLdb.connect (host= "127.0.0.1", user= "root", \

Passwd= "123456", db= "test", \

port=3306,charset= "UTF8")

Cur=conn.cursor ()

N=cur.execute (Sql,param)

Cur.close ()

Conn.commit ()

Connrollback ()

Conn.close ()

Third, Python operation mysql-Insert Data

1. Insert a data directly

Cur.execute ("INSERT into MSG (title,name,cotent) VALUES (' Python ', ' zz ', ' Test MySQL insert ')")

2. Inserting more than one data

# Use the Executemany () method to insert multiple values at a time, execute a single SQL statement, but repeat the parameters in the list of parameters, the return value is the number of rows affected

Iv. python operation mysql-querying data

1, Fetchall (): Receive all the returned result rows

2, Fetchmany (Size=none): Receives the size bar to return the result row, if the value of size is greater than the number of result rows returned, the cursor.arraysize data is returned

3, Fetchone (): Returns a result row

4, scroll (value,mode= ' relative '): Move the pointer to a line, if the mode= ' relative ', the current row is moving the value bar, if mode= ' absolute ', Moves the value bar from the first row of the result set

V. Python Operations mysql-Execution transactions

Transaction mechanisms ensure data consistency.

A transaction has four properties: atomicity, consistency, isolation, persistence.

These four properties are often called acid properties.

Python DB API 2.0 Transaction provides two methods of commit or rollback

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.