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