文档主页
MySQL Connector/Python 开发者指南
相关文档 下载本手册
PDF (US Ltr) - 0.7Mb
PDF (A4) - 0.7Mb


10.5.15 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