如何使用PYODBC导出Microsoft Access数据库中所有视图的SQL代码?

发布于 2025-01-24 18:28:34 字数 1192 浏览 2 评论 0原文

我正在尝试使用 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 技术交流群。

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

发布评论

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

评论(1

一瞬间的火花 2025-01-31 18:28:34

可能,后端ODBC-ACCESS连接不会在MSYS*表之外的访问中显示元数据。但是,您可以访问访问的前端。考虑与访问对象库接口访问 querydefs 收集以检索所有基础SQL语句。

Python的Win32COM通常是为python的Windows安装而预装的。另外,下面的工作可能甚至可以安装MSACCESS。您只需要可用的dao库(.dll)即可。

import win32com.client

try:
    dbe = win32com.client.Dispatch("DAO.DBEngine.120")
    db = dbe.OpenDatabase(r"C:\Path\To\Access\Database.accdb")

    # ITERATE THROUGH ALL SAVED QUERIES
    for qdef in db.QueryDefs():
        print(qdef.Name)
        print(qdef.SQL)
        print()
        
except Exception as e:
    print(e)
    
finally:
    # RELEASE RESOURCES
    qdef = None; db = None; dbe = None
    del qdef; del db; del dbe

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.

import win32com.client

try:
    dbe = win32com.client.Dispatch("DAO.DBEngine.120")
    db = dbe.OpenDatabase(r"C:\Path\To\Access\Database.accdb")

    # ITERATE THROUGH ALL SAVED QUERIES
    for qdef in db.QueryDefs():
        print(qdef.Name)
        print(qdef.SQL)
        print()
        
except Exception as e:
    print(e)
    
finally:
    # RELEASE RESOURCES
    qdef = None; db = None; dbe = None
    del qdef; del db; del dbe
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文