如何获取列类型宽度MySQLdb?

qqrboqgw  于 2023-11-16  发布在  Mysql
关注(0)|答案(4)|浏览(121)

我怎样才能得到文本描述列类型与MySQLdb?
我知道cursor.description包含代表列类型的数字,并且还有一个带有int常量的模块FIELD_TYPE.*,如FIELD_TYPE.ENUM=247
例如,如果我知道列类型是“3”,我如何获取列类型的名称?

bihw5rsg

bihw5rsg1#

请阅读这里的列类型列表:
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' }

字符串

qybjjes1

qybjjes12#

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的代码

uelo1irk

uelo1irk3#

select * from information_schema.tables

字符串
包含了你想知道的关于表格的一切:).. example output(我使用了\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;显示您想了解的有关列的所有信息。

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)

h5qlskok

h5qlskok4#

我不喜欢使用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

相关问题