10.5.12 MySQLCursor.description 属性

句法:

tuples = cursor.description

此只读属性返回描述结果集中列的元组列表。列表中的每个元组包含如下值:

(column_name,
 type,
 None,
 None,
 None,
 None,
 null_ok,
 column_flags)

以下示例显示如何解释 description元组:

import mysql.connector
from mysql.connector import FieldType

...

cursor.execute("SELECT emp_no, last_name, hire_date "
               "FROM employees WHERE emp_no = %s", (123,))
for i in range(len(cursor.description)):
  print("Column {}:".format(i+1))
  desc = cursor.description[i]
  print("  column_name = {}".format(desc[0]))
  print("  type = {} ({})".format(desc[1], FieldType.get_info(desc[1])))
  print("  null_ok = {}".format(desc[6]))
  print("  column_flags = {}".format(desc[7]))

输出如下所示:

Column 1:
  column_name = emp_no
  type = 3 (LONG)
  null_ok = 0
  column_flags = 20483
Column 2:
  column_name = last_name
  type = 253 (VAR_STRING)
  null_ok = 0
  column_flags = 4097
Column 3:
  column_name = hire_date
  type = 10 (DATE)
  null_ok = 0
  column_flags = 4225

column_flags值是 constants.FieldFlag该类的一个实例。要查看如何解释它,请执行以下操作:

>>> from mysql.connector import FieldFlag
>>> FieldFlag.desc