光标.描述“类型代码”与数据库字段类型的对应关系

发布于 2024-12-10 15:19:35 字数 377 浏览 1 评论 0原文

使用 MySQL 后端,基本上想要从游标.描述元组中的 type_code 确定表的字段类型...

我得到的是一堆不同的数字...并通过将我的表与 type_code 值进行比较可以手动组合一组对应关系...但是我的类型比我的 Python 书(Beazley)中记录的类型对象多得多,即 STRING、BINARY、NUMBER、DATETIME、ROWID。

我认为因此有不同的 type_codes 被赋予诸如 DECIMAL、UNSIGNED INT 等之类的东西......但我只是惊讶地无法在这里或在网上找到任何信息。

顺便说一句,我想做的是自动化这个过程,通过输入(例如连接到 MySQL 表的 GUI 网格中)确定表期望该列的数据类型,并解析和检查它以找到确定这是否是合法值。

Using a MySQL back-end and basically want to determine the field type of tables from the type_code in the cursor.description tuples...

What I get is a bunch of different numbers... and by comparing my tables with the type_code values I can manually put together a set of correspondences... but I have many more types than the type objects documented in my Python book (Beazley), namely STRING, BINARY, NUMBER, DATETIME, ROWID.

I presume there are therefore different type_codes being given to things like DECIMAL, UNSIGNED INT, etc... but I'm just surprised not to be able to find any info out here or on the Net generally.

What I want to do, by the way, is to automate the process whereby input (in a GUI grid connected to a MySQL table for example) determines what type of data the table is expecting for that column, and parses and checks it to find out whether this is a legal value.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦中的蝴蝶 2024-12-17 15:19:35

如果您有兴趣获取有关 ids 如何映射到描述的字典:

>>> ft = MySQLdb.constants.FIELD_TYPE
>>> d = {getattr(ft, k): k for k in dir(ft) if not k.startswith('_')}
>>> d
{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'}

If you are interested in getting a dictionary of how ids map to descriptions:

>>> ft = MySQLdb.constants.FIELD_TYPE
>>> d = {getattr(ft, k): k for k in dir(ft) if not k.startswith('_')}
>>> d
{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'}
桃气十足 2024-12-17 15:19:35

您书中描述的基本类型代码是由 DB-API 规范

type_code 必须等于下面定义的类型对象之一。

这里的技巧是,可以有多个不同的类型代码,所有这些代码都等于相同类型的对象。

>>> MySQLdb.constants.FIELD_TYPE.TIMESTAMP
7
>>> MySQLdb.constants.FIELD_TYPE.DATETIME
12
>>> MySQLdb.constants.FIELD_TYPE.TIMESTAMP==MySQLdb.DATETIME
True
>>> MySQLdb.constants.FIELD_TYPE.DATETIME==MySQLdb.DATETIME
True
>>> MySQLdb.DATETIME
DBAPISet([12, 7])

(DB-API 规范中有关 DBAPITypeObject 的注释中概述了如何实现这种魔法。更传统的接口可能通过子类来完成此操作...)

这允许 MySQLdb 提供有关列的更丰富的信息,而不仅仅是它是否是日期-and time 类型,同时仍然允许对字符串与数字进行简单测试。

当然,如果您开始直接与 MySQLdb.constants.FIELD_TYPE 类型进行比较,您将依赖于无法移植到其他数据库的 MySQLdb 功能。

The basic type codes as described in your book are defined by the DB-API specification.

The type_code must compare equal to one of Type Objects defined below.

The trick here is that there can be multiple different type codes that all compare equal to the same type object.

>>> MySQLdb.constants.FIELD_TYPE.TIMESTAMP
7
>>> MySQLdb.constants.FIELD_TYPE.DATETIME
12
>>> MySQLdb.constants.FIELD_TYPE.TIMESTAMP==MySQLdb.DATETIME
True
>>> MySQLdb.constants.FIELD_TYPE.DATETIME==MySQLdb.DATETIME
True
>>> MySQLdb.DATETIME
DBAPISet([12, 7])

(How this magic is implemented is outlined in the note about DBAPITypeObject in the DB-API spec. A more conventional interface might have done this with subclasses...)

This allows MySQLdb to offer richer information about the column than just whether it's a date-and-time type, whilst still allowing a simple test for a string vs a number.

Of course if you start comparing against MySQLdb.constants.FIELD_TYPE types directly you are relying on MySQLdb functionality that won't port to other databases.

横笛休吹塞上声 2024-12-17 15:19:35

如果您使用 MySQLdb,则 MySQLdb.constants.FIELD_TYPE 模块包含每种字段类型的常量。

>>> print dir(MySQLdb.constants.FIELD_TYPE)
['BIT', 'BLOB', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'DOUBLE', 'ENUM', 
 'FLOAT', 'GEOMETRY', 'INT24', 'INTERVAL', 'LONG', 'LONGLONG', 'LONG_BLOB', 
 'MEDIUM_BLOB', 'NEWDATE', 'NEWDECIMAL', 'NULL', 'SET', 'SHORT', 'STRING', 
 'TIME', 'TIMESTAMP', 'TINY', 'TINY_BLOB', 'VARCHAR', 'VAR_STRING', 'YEAR',
 '__builtins__', '__doc__', '__file__', '__name__', '__package__']

例如,5 的类型代码表示它是 MySQL double

>>> MySQLdb.constants.FIELD_TYPE.DOUBLE 
5

此模块在 文档

If you are using MySQLdb, then the MySQLdb.constants.FIELD_TYPE module contains constants for each field type.

>>> print dir(MySQLdb.constants.FIELD_TYPE)
['BIT', 'BLOB', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'DOUBLE', 'ENUM', 
 'FLOAT', 'GEOMETRY', 'INT24', 'INTERVAL', 'LONG', 'LONGLONG', 'LONG_BLOB', 
 'MEDIUM_BLOB', 'NEWDATE', 'NEWDECIMAL', 'NULL', 'SET', 'SHORT', 'STRING', 
 'TIME', 'TIMESTAMP', 'TINY', 'TINY_BLOB', 'VARCHAR', 'VAR_STRING', 'YEAR',
 '__builtins__', '__doc__', '__file__', '__name__', '__package__']

For example, the a typecode of 5 indicates it is a MySQL double

>>> MySQLdb.constants.FIELD_TYPE.DOUBLE 
5

This module is noted in the documentation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文