Download the installation module
PIP3 install MySQL
Use action
1. Using SQL statements in Python
ImportPymysqlconn= Pymysql.connect (host='127.0.0.1', port=3306, user='Root', passwd='0123', db='test1', charset='UTF8')#Create a connectioncursor= Conn.cursor ()#Creating CursorsEffect_row= Cursor.execute ('INSERT INTO User_info (Name,part_nid) VALUES ("Xiaohei", "1")')#Querying DataConn.commit ()#Commit ActionR= Cursor.execute ('SELECT * from User_info where nid= "1"') Conn.commit ()Print(R, Cursor.fetchall ())#outputs the number of rows affected and the result of the executioncursor.scroll (0, mode='Absolute')#set the position of the internal pointer, absolute position Bear 0 startsPrint(R, Cursor.fetchone ()) a= Cursor.execute ('SELECT * from User_info where nid in (3,5)') Conn.commit ()Print(A, Cursor.fetchall ()) Cursor.close ()#Close Cursorsconn.close ()#Close ConnectionView Code
2. Cursor set to dictionary type
1 cursor = conn.cursor (cursor=pymysql.cursors.dictcursor)
3. Get the newly created data self-increment ID
1 new_id = Cursor.lastrowid
4. About FETCH
Fetchall ()# get all data fetchone ()# get the first bar of fetchmany (n)# get more than one
View Code
Python operation MySQL