pymysql module connection operation mysql_mysql

import pymysql #Create 

connection channel
conn = pymysql.connect (host = '', user = 'root', port = 3306, password = 'thinker', db = 'testdb') #Generate cursor for
operation mysql
cursor = conn.cursor ()

# Get data
effect_row = cursor.execute ( 'select * from teacher') # and returns the affected functions

print (cursor.fetchone ()) # print a data

cursor.scroll (1, mode = 'relative ') # Review Cursor position, relative relative position movement
cursor.scroll (0, mode = "absolute") #Modify cursor position, absolute absolute position movement, 0 means return to the beginning

print (cursor.fetchmany (2)) #Print the specified bar数 数
print (cursor.fetchall ()) # Print all remaining data


# Insert a row of data
cursor.execute ("insert into teacher (teacher_name) values ​​('chenxiaozan')") #Insert
multiple rows of data
cursor.executemany ("insert into teacher (teacher_name) values ​​(% s) ", ['lucas', 'eric', 'loss']) #Note that there is some difference between the% s of Python string stitching, here is the data

conn.commit () #Confirm to submit
cursor.close () #Close the cursor
conn.close () #Close the connection
new_id = cursor.lastrowid #When inserting data, insert the latest auto increment id
print (new_id)

Guess you like

Origin www.cnblogs.com/chenxiaozan/p/12682797.html