简体   繁体   English

读取MDF文件int Pandas DataFrame

[英]Read MDF file int Pandas DataFrame

I'm attempting to read a Microsoft SQL Server MDF file into a Python pandas DataFrame. 我正在尝试将Microsoft SQL Server MDF文件读取到Python pandas DataFrame中。 I'm extremely lost on the subject and would really appreciate any push in the right direction. 我在这个问题上迷失了方向,非常感谢朝着正确方向的推动。 Please let me know any information that would make answering this question easier. 请让我知道任何可以简化回答此问题的信息。

Here are some of the resources I've found but can't seam to bring to a finished product: https://pandas.pydata.org/pandas-docs/stable/io.html#engine-connection-examples http://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_sql.html Python open Microsoft SQL Server MDF file 以下是一些我发现的资源,但无法缝制成成品: https : //pandas.pydata.org/pandas-docs/stable/io.html#engine-connection-examples http:// /pandas.pydata.org/pandas-docs/stable/generation/pandas.read_sql.html Python打开Microsoft SQL Server MDF文件

I had to 1.) attach the .mdf file to my local instance of sql server via sql server studio and then 2.) open a port on my computer to allow read/write access to sql server. 我必须1.)通过SQL Server Studio将.mdf文件附加到我的SQL Server本地实例,然后2.)在计算机上打开一个端口,以允许对SQL Server进行读/写访问。 If we can make the .mdf available on sql server, then we can connect to it via pandas/sqlAlchemy with pd.read_sql() and the sql server connection string. 如果我们可以使.mdf在sql服务器上可用,那么我们可以使用pd.read_sql()和sql服务器连接字符串通过pandas / sqlAlchemy连接到它。

  1. Go to the folder of your .mdf file, and share the file with everyone on the machine. 转到.mdf文件的文件夹,然后与计算机上的所有人共享该文件。 On windows 10, I did this by right clicking the file, highlighting Give access to , and selecting Specific people... . 在Windows 10上,我通过右键单击文件,突出显示“ Give access to ,然后选择“ Specific people... I then added everyone from the drop down, giving them read/write permissions. 然后,我从下拉列表中添加了everyone ,为他们提供了读/写权限。 This step is important, as I could not attach the file to sql server otherwise. 这一步很重要,因为否则我无法将文件附加到sql服务器。 Now, open sql server management studio, connect to your instance of sql server, and right click Databases . 现在,打开sql server management studio,连接到sql server实例,然后右键单击Databases There you have an option Attach... where you can follow the menu to select your .mdf file. 那里有一个选项Attach... ,您可以按照菜单选择.mdf文件。 Your .mdf is now attached to sql server. 您的.mdf现在已附加到sql server。

  2. Now that your .mdf is in sql server, we still have the issue of being able to access it. 现在,您的.mdf在sql server中,我们仍然可以访问它。 This can be resolved by opening a port on your machine to allow access to sql server. 可以通过打开计算机上的端口以允许访问sql server的方法来解决。 I followed this guide to be able to do that: https://docs.microsoft.com/en-us/sql/relational-databases/lesson-2-connecting-from-another-computer?view=sql-server-2017 . 我遵循此指南可以执行以下操作: https : //docs.microsoft.com/zh-cn/sql/relational-databases/lesson-2-connecting-from-another-computer?view=sql-server-2017 Once you finish this how-to, your sql server should be open to access. 完成此操作方法后,应打开您的sql服务器以进行访问。 Now there may be easier ways to access sql server on a local machine, but the nice thing about this method is that it should also work if you want to read sql server from a remote machine, too. 现在可能有更简单的方法可以访问本地计算机上的sql server,但是这种方法的好处是,如果您也想从远程计算机上读取sql server,它也应该可以工作。 All you'll have to do is change the ip address in our connection string (below). 您要做的就是更改我们连接字符串中的IP地址(如下)。

Now that we have an accessible instance of sql server and our .mdf file as a database inside of it, we can read the file like we would any other database from pandas. 现在我们有了一个可访问的sql server实例,并且其中的.mdf文件作为其中的数据库,我们可以像读取pandas的任何其他数据库一样读取该文件。 I used the following setup to now read my .mdf file. 我使用以下设置现在读取我的.mdf文件。

# MS SQL Server Config
server = '127.0.0.1'  # mssql is set up on localhost
port = '1433'  # the port I opened to access mssql
database = 'database_name'
username = 'username'
password = 'password'
driver = 'SQL+SERVER'
schema = 'dbo'

# create a sqlAlchemy engine with the above credentials
connection_str = f'mssql+pyodbc://{server}:{port}/{database}?driver={driver}'
engine = create_engine(connection_str)

# read our mdf file!
query = 'select * from table_name;'
df = pd.read_sql(query, engine)

Now I don't have my sql server password protected since I am just running locally, but in case you have yours, you'll want to adjust your connection string as follows: 现在,由于我只是在本地运行,所以我的SQL Server密码没有受到保护,但是如果您拥有自己的SQL Server密码,则需要按以下方式调整连接字符串:

connection_str = f'mssql+pyodbc://{username}:{password}@{server}:{port}/{database}?driver={driver}'

With that, you should now have table_name from your .mdf file read into a pandas dataframe. 这样,您现在应该将.mdf文件中的table_name读入pandas数据框中。

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

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