简体   繁体   English

VBA 是否有一种 psycopg2 风格的方式来连接到 PostgreSQL?

[英]Does VBA have a psycopg2 style way to connect to PostgreSQL?

I have been running queries in SQL in Python using psycopg2.我一直在使用 psycopg2 在 Python 中的 SQL 中运行查询。 This is very simple and I just do the following:这非常简单,我只需执行以下操作:

conn2 = psycopg2.connect('postgresql://YourUserName:YourPassword@YourHost:5432/YourDatabase')
query = "xxx"
query_output = pd.read_sql_query(query, conn2).drop_duplicates(keep='first')

Is it possible to something similarly as easy in VBA?是否有可能在 VBA 中进行类似的简单操作? I am hoping to create an easy-to-use excel add-in so want to avoid asking the user to have to set up 32-bit drivers in ODBC Driver Manager.我希望创建一个易于使用的 excel 插件,因此希望避免要求用户必须在 ODBC 驱动程序管理器中设置 32 位驱动程序。

I am trying to develop a setup here:我正在尝试在这里开发一个设置:

Dim objDb_con
Dim strSomeValue As String

Set objDb_con = CreateObject("ADODB.Connection")
Set Rsdatatype = CreateObject("ADODB.RecordSet")

glbConnString = "Driver={PostgreSQL UNICODE};Database=XX;port=XX;server=XX;UID=XX;Pwd=XX;"

objDb_con.Open glbConnString

But I get the error:但我得到了错误:

[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified

Looking around it appears this is to do with ODBC driver managers and even though I tried to set-up the default as 32-bit I still get this error.环顾四周,这似乎与 ODBC 驱动程序管理器有关,即使我尝试将默认设置为 32 位,我仍然会收到此错误。

Is there a psycopg2-style alternative for VBA that is simple and doesn't require set-up for the user? VBA 是否有一种简单且不需要用户设置的 psycopg2 样式替代方案?

I always pull connection strings from this website:我总是从这个网站上提取连接字符串:

https://www.connectionstrings.com/progress/ https://www.connectionstrings.com/progress/

They clearly show that there is no built-in .NET libraries or OLE DB providers for PostGress.它们清楚地表明 PostGress 没有内置的 .NET 库或 OLE DB 提供程序。 Thus, you cannot get around installing the driver.因此,您无法绕过安装驱动程序。 You could package an installer to automate this process.您可以使用 package 安装程序来自动执行此过程。

If you have the ODBC drivers installed you would use a connection string like this:如果您安装了 ODBC 驱动程序,您将使用如下连接字符串:

Standard标准

Include only the parameters you want to override from the system DSN settings仅包括要从系统 DSN 设置中覆盖的参数

DSN=myDSN;HOST=myServerAddress;DB=myDataBase;UID=myUsername;PWD=myPassword;PORT=2055;

Alternative using long names使用长名称的替代方法

Include only the parameters you want to override from the system DSN settings仅包括要从系统 DSN 设置中覆盖的参数

DataSourceName=myDSN;HostName=myServerAddress;Database=myDataBase;LogonID=myUsername;Password=myPassword;PortNumber=2055;

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

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