如何使用PYODBC导出Microsoft Access数据库中所有视图的SQL代码?
我正在尝试使用 pyodbc 在Microsoft Access数据库中导出所有 Views 的SQL代码。我正在使用下面的代码访问所有表名称和类型,但是我无法弄清楚如何访问这些视图后面的SQL代码。我已经广泛地在线搜索,并浏览了PYODBC文档,但是我找不到有关如何执行此操作的任何解释。这是我到目前为止的代码:
import pandas as pd
db_path = 'path_to_database.accdb'
# Create connection to database
conn = pyodbc.connect(
r'Driver={Microsoft Access Driver (*.mdb, *.accdb)};'
+ 'DBQ={};Trusted_Connection=yes;'.format(db_path))
# Print the table types and names
with conn:
cursor = conn.cursor()
for row in cursor.tables():
print(row.table_type, row.table_name)
cursor.close()
conn.close()
示例输出:
SYNONYM Group List
...
SYSTEM TABLE MSysObjects
SYSTEM TABLE MSysQueries
...
TABLE Classification List
TABLE Financial_2022
VIEW All Programs
VIEW ERROR_Mismatch
VIEW FTEs
VIEW UNION_Cascade
我不知道Mysyssqueries System表是什么,我试图访问其中的信息,但我无法:记录无法读取;在“ msysqueries”上没有阅读权限。
我已经看到示例如何在SQL Server中执行此操作,但我不知道如何将其转换为PyodBC和Microsoft Access。
任何帮助都将受到赞赏!
I am trying to use pyodbc to export the SQL code of all the views in a Microsoft Access database. I am using the code below to access all the table names and types, but I cannot figure out how to access the SQL code behind these views. I have searched online extensively and looked through the pyodbc documentation, but I cannot find any explanation of how to do this. Here is my code so far:
import pandas as pd
db_path = 'path_to_database.accdb'
# Create connection to database
conn = pyodbc.connect(
r'Driver={Microsoft Access Driver (*.mdb, *.accdb)};'
+ 'DBQ={};Trusted_Connection=yes;'.format(db_path))
# Print the table types and names
with conn:
cursor = conn.cursor()
for row in cursor.tables():
print(row.table_type, row.table_name)
cursor.close()
conn.close()
Example output:
SYNONYM Group List
...
SYSTEM TABLE MSysObjects
SYSTEM TABLE MSysQueries
...
TABLE Classification List
TABLE Financial_2022
VIEW All Programs
VIEW ERROR_Mismatch
VIEW FTEs
VIEW UNION_Cascade
I don't know what the MySysQueries system table is, and I have tried to access the information inside of it but I cannot: Record(s) cannot be read; no read permission on 'MSysQueries.'
I have seen an example of how to do this in SQL Server, but I do not know how to translate that to pyodbc and Microsoft Access.
Any help is appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可能,后端ODBC-ACCESS连接不会在MSYS*表之外的访问中显示元数据。但是,您可以访问访问的前端。考虑与访问对象库接口访问 querydefs 收集以检索所有基础SQL语句。
Python的
Win32COM
通常是为python的Windows安装而预装的。另外,下面的工作可能甚至可以安装MSACCESS。您只需要可用的dao库(.dll)即可。Possibly, the backend ODBC-Access connection does not expose metadata on saved queries in Access outside the MSys* tables. However, you can access the frontend of Access. Consider interfacing to the Access Object Library to access the QueryDefs collection to retrieve all underlying SQL statements.
Python's
win32com
is usually pre-installed for Windows installations of Python. Also, below may work without even having MSAccess installed. You will just need the DAO library (.dll) available.