简体   繁体   English

Python打开Microsoft SQL Server MDF文件

[英]Python open Microsoft SQL Server MDF file

How can I open an Microsoft SQL Server MDF file in Python? 如何在Python中打开Microsoft SQL Server MDF文件?

Edit 编辑

I've tried pyodbc.connect but that requires a legitimate "server connection"—you can't simply open the MDF file— 我已经尝试过pyodbc.connect但这需要一个合法的“服务器连接”-您不能简单地打开MDF文件-

pyodbc.connect(driver='{SQL Server}', dbq=r'c:\database.mdf')

(Like you'd do for MDB files.) (就像您要处理MDB文件一样。)

pyodbc.Error: Neither DSN or SERVER keyword supplied pyodbc.Error:未提供DSN或SERVER关键字

Okay— 好的-

pyodbc.connect(driver='{SQL Server}', dsn=r'c:\database.mdf')

pyodbc.Error: Data source name not found pyodbc.Error:找不到数据源名称

All I want to do is list its schema and then it. 我要做的只是列出其架构,然后列出。

The fact that you're using Python isn't relevant here: an .mdf file is a binary database file that can only be accessed by attaching it to SQL Server and querying the database through the server instance. 您使用Python的事实与此处无关:.mdf文件是二进制数据库文件,只能通过附加到SQL Server并通过服务器实例查询数据库来访问。 It's not clear why this is an issue for you, because pyodbc will work fine with SQL Server (on Windows, at least) so if this doesn't answer your question you may want to expand on what you're trying to do and why you can't just connect to SQL Server. 目前尚不清楚为什么这对您来说是个问题,因为pyodbc可以在SQL Server上正常运行(至少在Windows上),因此,如果这不能回答您的问题,则可能需要扩展您尝试做的事情以及原因您不能仅连接到SQL Server。

And mentioning your OS, Python version, SQL Server version etc. is usually helpful. 并且提及您的操作系统,Python版本,SQL Server版本等通常会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM