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