简体   繁体   English

如何从VBA查询PostgreSQL

[英]How to query PostgreSQL from VBA

How can I query PostreSQL in VBA? 如何在VBA中查询PostreSQL? I have tried the following which I found online but doesn't seem to work. 我尝试了以下在网上发现的方法,但似乎没有用。

Sub query()

'Create Connection
Set conn = CreateObject("ADODB.Connection")
strCnx = "Driver={PostgreSQL UNICODE};Server=localhost;Database=databasename;uid=username;pwd=password123;"
conn.Open strCnx

'Query the Database
Set rs = CreateObject("ADODB.recordset")
rs.Open "select * from dm.acct_dim limit 10", conn

Record = rs.GetRows()
rs.Close
conn.Close

'Write results to file
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\dbData.txt", 8, True)

For Each Item In Record
  objFile.WriteLine (Item)
Next

objFile.Close

End Sub

When I try this i get an error saying "could not connect to the server; No Connection could be made because the target machine actively refused it." 当我尝试此操作时,我收到一条错误消息:“无法连接到服务器;由于目标计算机主动拒绝连接,因此无法建立连接”。

When I connect with pgadmin i use host, port, database, username and password. 当我与pgadmin连接时,我使用主机,端口,数据库,用户名和密码。 So I'm guessing i need those same things in the connection string but not exactly sure how that should be setup. 所以我猜我在连接字符串中也需要这些相同的东西,但不能完全确定应该如何设置。 I'm a rookie here. 我是这里的菜鸟。 thanks for any help. 谢谢你的帮助。

Looks like it was correct as it was. 看起来它是正确的。 My problem was that I had wrong server. 我的问题是服务器错误。 My bad. 我的错。 Thanks for trying to help! 感谢您的帮助!

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

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