我怎样才能获得MySQLdb的列类型宽度?

时间:2010-09-26 15:18:40

标签: python mysql

如何使用MySQLdb获取列类型的文本描述?

我知道cursor.description包含代表列类型的数字 还有模块FIELD_TYPE.*,其中包含FIELD_TYPE.ENUM=247

等int常量

例如,如果我知道它是'3',我如何得到列类型的名称?

4 个答案:

答案 0 :(得分:4)

请在此处阅读列类型列表:

http://mysql-python.sourceforge.net/MySQLdb-1.2.2/public/MySQLdb.constants.FIELD_TYPE-module.html

然后从提供的列表中创建一个字典:

field_type = {
 0: 'DECIMAL',
 1: 'TINY',
 2: 'SHORT',
 3: 'LONG',
 4: 'FLOAT',
 5: 'DOUBLE',
 6: 'NULL',
 7: 'TIMESTAMP',
 8: 'LONGLONG',
 9: 'INT24',
 10: 'DATE',
 11: 'TIME',
 12: 'DATETIME',
 13: 'YEAR',
 14: 'NEWDATE',
 15: 'VARCHAR',
 16: 'BIT',
 246: 'NEWDECIMAL',
 247: 'INTERVAL',
 248: 'SET',
 249: 'TINY_BLOB',
 250: 'MEDIUM_BLOB',
 251: 'LONG_BLOB',
 252: 'BLOB',
 253: 'VAR_STRING',
 254: 'STRING',
 255: 'GEOMETRY' }

答案 1 :(得分:0)

select * from information_schema.tables

包含你想知道的关于表的所有内容:) ..示例输出(我使用\ G开关进行更漂亮的格式化):

....
*************************** 87. row ***************************
  TABLE_CATALOG: NULL
   TABLE_SCHEMA: phpmyadmin
     TABLE_NAME: pma_table_coords
     TABLE_TYPE: BASE TABLE
         ENGINE: MyISAM
        VERSION: 10
     ROW_FORMAT: Dynamic
     TABLE_ROWS: 29
 AVG_ROW_LENGTH: 39
    DATA_LENGTH: 1156
MAX_DATA_LENGTH: 281474976710655
   INDEX_LENGTH: 4096
      DATA_FREE: 0
 AUTO_INCREMENT: NULL
    CREATE_TIME: 2010-02-09 11:19:59
    UPDATE_TIME: 2010-08-15 19:21:37
     CHECK_TIME: 2010-08-16 18:34:18
TABLE_COLLATION: utf8_bin
       CHECKSUM: NULL
 CREATE_OPTIONS: 
  TABLE_COMMENT: Table coordinates for phpMyAdmin PDF output
...

SHOW COLUMNS FROM table;显示你想知道的关于columsn的一切......

mysql> show columns from pma_designer_coords;
+------------+-------------+------+-----+---------+-------+
| Field      | Type        | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| db_name    | varchar(64) | NO   | PRI |         |       |
| table_name | varchar(64) | NO   | PRI |         |       |
| x          | int(11)     | YES  |     | NULL    |       |
| y          | int(11)     | YES  |     | NULL    |       |
| v          | tinyint(4)  | YES  |     | NULL    |       |
| h          | tinyint(4)  | YES  |     | NULL    |       |
+------------+-------------+------+-----+---------+-------+
6 rows in set (0.00 sec)

答案 2 :(得分:0)

我不喜欢使用dir(),这个模块确实应该修复(使用枚举),但至少不要重复。

import m

name_to_value = {
    k: getattr(m, k)
    for k in dir(m)
    if k.isupper()
}

value_to_name = {
    v: k
    for k, v in name_to_value.items()
}

print(value_to_name[42])

https://repl.it/@altendky/CruelSnoopyCharactercode

答案 3 :(得分:-1)

mysql.connector在FieldType的get_info()方法中提供了此信息:

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]))
来自http://dev.mysql.com/doc/connector-python/en/connector-python-api-mysqlcursor-description.html

代码