光标.描述“类型代码”与数据库字段类型的对应关系
使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您有兴趣获取有关 ids 如何映射到描述的字典:
If you are interested in getting a dictionary of how ids map to descriptions:
您书中描述的基本类型代码是由 DB-API 规范。
这里的技巧是,可以有多个不同的类型代码,所有这些代码都等于相同类型的对象。
(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 trick here is that there can be multiple different type codes that all compare equal to the same type object.
(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.
如果您使用 MySQLdb,则 MySQLdb.constants.FIELD_TYPE 模块包含每种字段类型的常量。
例如,
5
的类型代码表示它是 MySQL double此模块在 文档。
If you are using
MySQLdb
, then theMySQLdb.constants.FIELD_TYPE
module contains constants for each field type.For example, the a typecode of
5
indicates it is a MySQL doubleThis module is noted in the documentation.