In Python, how does one obtain the field list after the query result of the module pymysql?

Source: Internet
Author: User

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.descriptionYou 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.rowcountReturns 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.