SoFunction
Updated on 2024-10-28

Python Tips for using pymysql

In the use of pymysql, through fetchall () or fetchone () can get the query results, but this return data does not contain field information (not as convenient as php). Check pymysql source code, in fact, get the query results source code is also very simple, direct calls can be.

For example:

db = (...)
cur = ()
(sql)
print()
result = ()
data_dict=[]
for field in :
  data_dict.append(field[0])
print(data_dict)

In pymysql's pymysql/, find class Cursor to see the following code:

def __init__(self, connection):
   = connection
   = None
   = 0
   = -1
   = 1
  self._executed = None
  self._result = None
  self._rows = None
  self._warnings_handled = False

Therefore, the call is able to quickly return the number of rows of the query result without having to get it from len().