In Python, how does one obtain the field list after the query result of the module pymysql?
Preface
When using pymysqlfetchall()
Orfetchone()
The query results can be obtained, but the returned data does not contain field information (not as convenient as php ). After checking the pymysql source code, the source code of the query result is also very simple and can be called directly.cursor.description
You can.
For example:
db = pymysql.connect(...)cur = db.cursor()cur.execute(sql)print(cur.description)result = cur.fetchall()data_dict=[]for field in cur.description: data_dict.append(field[0])print(data_dict)
In pymysql/cursors. py of pymysql, find the class Cursor and you can see the following code:
def __init__(self, connection): self.connection = connection self.description = None self.rownumber = 0 self.rowcount = -1 self.arraysize = 1 self._executed = None self._result = None self._rows = None self._warnings_handled = False
Thereforecur.rowcount
Returns the number of query results quickly without passinglen()
.
Summary
Well, let's talk about it first. The above is all the content of this article. I hope the content of this article will help you in your study or work. If you have any questions, please leave a message, thank you for your support.