如何使用pyodbc获取Access数据库特定字段的数据类型?
我正在使用 pyodbc 对 .mbd (访问)文件中的大型数据库进行数据挖掘。
我想创建一个新表,从多个现有表中获取相关信息(然后将其提供给工具)。
我想我知道传输数据所需的一切,并且知道如何创建给定列名称和数据类型的表,但是我在获取相应列的数据类型(INTEGER、VARCHAR 等)时遇到问题在现有表中。我需要这些类型来兼容地创建新列。
我在互联网上找到的内容(例如 此 和此 )让我陷入无效命令的麻烦,所以我认为这是一个特定于平台的问题。话又说回来,我对数据库还很陌生。
有人知道如何获取这些字段的类型吗?
I'm using pyodbc to data-mine a big database in a .mbd (access) file.
I want to create a new table taking relevant information from several existing tables (to then feed it to a tool).
I think I know all I need to transfer the data, and I know how to create a table given column names and datatypes, but I'm having trouble getting the datatypes (INTEGER, VARCHAR, etc.) of the respective columns in the existing tables. I need these types to create the new columns compatibly.
What I found on the internet (like this and this) is getting me into invalid-command trouble, so I think this is a platform-specific issue. Then again, I'm fairly green on databases.
Does anybody know how to get the types of these fields?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这些文章对您没有帮助的原因是它们适用于 SQL Server。 SQL Server 具有可以查询以获取列数据的系统表,而 MS Access 则没有。 MS Access 仅允许您查询对象名称。
但是 ODBC 确实支持通过 ODBC.SQLColumns 函数。
根据 这个答案 PyODBC 通过游标方法
正如马克在评论中指出的那样,您可能还需要 row.data_type。他提供的链接包含其提供的所有列
The reason why those articles aren't helping you is because they are for SQL Server. SQL Server has system tables that you can query to get the column data, MS Access doesn't. MS Access only lets you query the object names.
However ODBC does support getting the schema through its connection via the ODBC.SQLColumns functions.
According to this answer PyODBC exposes this via a cursor method
As Mark noted in the comments you probably also want the row.data_type. The link he provided includes all the columns it provides
我对 pyodbc 不熟悉,但我过去在 VBA 中做过这个。
您提到的 2 个链接适用于 SQL Server,不适用于 Access。要找出Access表中每个字段的数据类型,可以使用DAO或ADOX。
这是我在 Excel 2010 中使用 VBA 所做的一个示例,其中我连接到 Access 数据库(2000 mdb 格式)并列出表、字段及其数据类型(作为枚举,例如“4”表示 dbLong)。您可以在输出中看到系统表,并在底部看到用户创建的表。
您可以轻松地在互联网上找到有关如何使用 ADOX 执行类似操作的示例。我希望这有帮助。
I am not familiar with pyodbc, but I have done this in VBA in the past.
The 2 links you mentionned are for SQL Server, not for Access. To find out the data type of each field in an Access table, you can use DAO or ADOX.
Here is an example I did, in VBA with Excel 2010, where I connect to the Access database (2000 mdb format) and list the tables, fields and their datatypes (as an enum, for example '4' means dbLong). You can see in the output the system tables and, at the bottom, tables created by the user.
You can easily find examples on internet for how to do something similar with ADOX. I Hope this helps.
您将从以下输出中获得一些信息:
You'll get some info from this output: