使用PyODBC在表中选择列名称 [英] Select column names in a table using PyODBC
问题描述
我正在编写一个Python程序,该程序使用PyODBC从Microsoft Access mdb文件中选择一些数据.
I'm writing a Python program that selects some data from a Microsoft Access mdb file using PyODBC.
我需要发现几个不同表的列名.在SQL Server中,这可以通过使用类似
I need to discover the column names of several different tables. In SQL Server, this can be accomplished by using a query like
SELECT c.name FROM sys.columns c, sys.tables t
WHERE c.object_id = t.object_id
AND t.name = tableName
但是该查询在Access中不起作用.与
But that query doesn't work in Access. With
SELECT MSysObjects.Name FROM MSysObjects
WHERE (((MSysObjects.Flags)=0) AND ((MSysObjects.Type)=1))
ORDER BY MSysObjects.Name
我可以获得非链接表名的列表,但是MSysObject似乎没有包含列名的列表.
I can get a list of non-linked table names, but MSysObject doesn't seem to contain a list of column names.
是否可以使用SQL来获取Access数据库中表的列名?
Is there a way to use SQL to grab the column names of a table in an Access database?
推荐答案
我无法找到SQL查询来完成此任务.但是,我确实发现PyODB有一个游标方法可以返回列列表
I was unable to find an SQL query to accomplish this. However, I did discover that PyODB has a cursor method that can return a list of columns
# columns in table x
for row in cursor.columns(table='x'):
print row.column_name
这篇关于使用PyODBC在表中选择列名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!