如何获取列类型宽度MySQLdb?

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

我怎样才能得到文本描述列类型与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
然后从提供的列表中创建一个字典:

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

字符串

展开查看全部
qybjjes1

qybjjes12#

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

  1. import mysql.connector
  2. from mysql.connector import FieldType
  3. ...
  4. cursor.execute("SELECT emp_no, last_name, hire_date "
  5. "FROM employees WHERE emp_no = %s", (123,))
  6. for i in range(len(cursor.description)):
  7. print("Column {}:".format(i+1))
  8. desc = cursor.description[i]
  9. print("column_name = {}".format(desc[0]))
  10. print("type = {} ({})".format(desc[1], FieldType.get_info(desc[1])))
  11. print("null_ok = {}".format(desc[6]))
  12. print("column_flags = {}".format(desc[7]))

字符串
来自http://dev.mysql.com/doc/connector-python/en/connector-python-api-mysqlcursor-description.html的代码

展开查看全部
uelo1irk

uelo1irk3#

  1. select * from information_schema.tables

字符串
包含了你想知道的关于表格的一切:).. example output(我使用了\G开关来进行更漂亮的格式化):

  1. ....
  2. *************************** 87. row ***************************
  3. TABLE_CATALOG: NULL
  4. TABLE_SCHEMA: phpmyadmin
  5. TABLE_NAME: pma_table_coords
  6. TABLE_TYPE: BASE TABLE
  7. ENGINE: MyISAM
  8. VERSION: 10
  9. ROW_FORMAT: Dynamic
  10. TABLE_ROWS: 29
  11. AVG_ROW_LENGTH: 39
  12. DATA_LENGTH: 1156
  13. MAX_DATA_LENGTH: 281474976710655
  14. INDEX_LENGTH: 4096
  15. DATA_FREE: 0
  16. AUTO_INCREMENT: NULL
  17. CREATE_TIME: 2010-02-09 11:19:59
  18. UPDATE_TIME: 2010-08-15 19:21:37
  19. CHECK_TIME: 2010-08-16 18:34:18
  20. TABLE_COLLATION: utf8_bin
  21. CHECKSUM: NULL
  22. CREATE_OPTIONS:
  23. TABLE_COMMENT: Table coordinates for phpMyAdmin PDF output
  24. ...


SHOW COLUMNS FROM table;显示您想了解的有关列的所有信息。

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

展开查看全部
h5qlskok

h5qlskok4#

我不喜欢使用dir(),这个模块真的应该被修复(使用枚举),但至少不要重复的东西。

  1. import m
  2. name_to_value = {
  3. k: getattr(m, k)
  4. for k in dir(m)
  5. if k.isupper()
  6. }
  7. value_to_name = {
  8. v: k
  9. for k, v in name_to_value.items()
  10. }
  11. print(value_to_name[42])

字符串
https://repl.it/@altendky/CruelSnoopyCharactercode

展开查看全部

相关问题