简体   繁体   English

使用 Pyodbc 从 WSL2 连接到 Windows 上的数据库的 DSN 连接

[英]Connect with DSN Connection to Database on Windows from WSL2 with pyodbc

I am looking to use a DSN connection that has been set up on my Windows 10 machine to pull data from our Sage100 Financial database.我希望使用在我的 Windows 10 机器上设置的 DSN 连接从我们的 Sage100 Financial 数据库中提取数据。 I know the DSN connection is set up correctly as I'm able to connect to the database & refresh data in Microsoft Excel using the "Get Data" query option.我知道 DSN 连接设置正确,因为我能够使用“获取数据”查询选项连接到 Microsoft Excel 中的数据库和刷新数据。 I currently open Excel, refresh the tables, & then use Python in WSL2 to automate reports.我目前打开 Excel,刷新表格,然后在 WSL2 中使用 Python 来自动生成报告。

I would like to remove the Excel / refresh table portion of the workflow & connect to the database in VS Code in WSL2.我想删除工作流的 Excel / 刷新表部分并连接到 WSL2 中 VS Code 中的数据库。

In the past, I have utilized Python / pyodbc to query the database with success using Python on the windows side of my machine using the below.过去,我使用 Python / pyodbc 在我机器的 windows 端使用 Python 成功查询数据库,使用以下命令。 This reinforces the DSN connection works on the Windows side.这加强了 Windows 端的 DSN 连接工作。

import pyodbc
import pandas as pandas

cnxn = pyodbc.connect("DSN=KGConnection", autocommit=True)
sql = """ SELECT * FROM IM_ItemWarehouse """

x = pd.read_sql(sql,cnxn)

My goal however is to use WSL2 for all queries & further analysis.然而,我的目标是使用 WSL2 进行所有查询和进一步分析。 I found this StackOverflow link that gets close to the correct answer, but I am hoping to find something that allows me to utilize the simple DSN that I know works.我发现这个 StackOverflow 链接接近正确答案,但我希望找到能让我利用我知道有效的简单 DSN 的东西。

Any help would be greatly appreciated!任何帮助将不胜感激!

TL;DR - pyodbc on WSL2 cannot use ODBC DSNs defined on the Windows side. TL;DR - WSL2 上的 pyodbc 不能使用在 Windows 端定义的 ODBC DSN。

That is because那是因为

  1. the ODBC Driver Manager on WSL2 (unixODBC) cannot "see" the Windows DSNs, and WSL2 (unixODBC) 上的 ODBC 驱动程序管理器无法“看到”Windows DSN,并且

  2. even if they were visible, the Windows ODBC DSNs would use the Windows version of the ODBC driver (eg, "msodbcsql17.dll" for MS SQL Server) and not the Linux version of the driver (eg, "libmsodbcsql-17.10.so.2.1"), and Linux cannot natively run Windows DLLs. even if they were visible, the Windows ODBC DSNs would use the Windows version of the ODBC driver (eg, "msodbcsql17.dll" for MS SQL Server) and not the Linux version of the driver (eg, "libmsodbcsql-17.10.so. 2.1"),而 Linux 无法在本机运行 Windows DLL。

If you want to establish a direct ODBC connection to a database (ie, any database, regardless of the platform on which it is hosted) from WSL2 you will need to install a Linux version of the ODBC driver for that database.如果您想从 WSL2 建立到数据库(即任何数据库,无论其托管平台如何)的直接 ODBC 连接,您需要为该数据库安装 Linux 版本的 ODBC 驱动程序。

If the database in question does not have a Linux ODBC driver but does have a Windows ODBC driver then it might be possible to create an indirect connection by如果有问题的数据库没有 Linux ODBC 驱动程序但有 Windows ODBC 驱动程序,则可以通过以下方式创建间接连接

  • installing MS SQL Server Express Edition on Windows,在 Windows 上安装 MS SQL Server Express Edition,
  • creating a SQL Server "linked server" to the other database via the Windows ODBC driver for that database, and then通过该数据库的 Windows ODBC 驱动程序创建到另一个数据库的 SQL 服务器“链接服务器”,然后
  • accessing the linked server via the Linux version of the MS SQL Server ODBC driver on WSL2通过 WSL2 上的 MS SQL 服务器 ODBC 驱动程序的 Linux 版本访问链接服务器

but that might easily prove to be more trouble than it's worth.但这可能很容易证明比它的价值更麻烦。

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

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