2

我正在将数据库应用程序设置为与数据库无关,并且在使用 postgresql 进行测试时出现标准 dsn 错误:

[IM002] [Microsoft][ODBC Driver Manager] 未找到数据源名称

我通常使用 SQL server 和 MySQL,所以我是 postgres 的新手,我尝试了标准推荐的连接字符串:

"Driver = {PostgreSQL}; Server = localhost; Database = postgres; Port = 5432; Uid = postgres; Pwd = XXXXXX;"

我还尝试了安装 postrgesql 后安装的 odbc 驱动程序的名称:

"Driver = {PostgreSQL ODBC Driver(UNICODE)}; Server = localhost; Database = postgres; Port = 5432; Uid = postgres; Pwd = XXXXXX;"

在 odbc 管理器中设置 DSN 也可以完美地使用 unicode 驱动程序,所以我无法理解为什么我无法在我的应用程序中连接,我在连接字符串中使用的驱动程序名称是否有错误?

4

2 回答 2

1

创建 DSN 时,是否使用正确的 odbcad 工具创建它?C:\Windows\System32如果您的应用程序是 64 位,则找到 64 位版本,如果您的应用程序是 32 位,则找到 32 位版本C:\Windows\SysWOW64

于 2016-01-14T12:56:55.670 回答
1

你的错误信息看起来很奇怪。它告诉 DSN 未找到。你确定你使用连接字符串Driver=...吗?

您可以以多种形式使用 ODBC 连接字符串。首先你已经创建了 DSN,所以你可以使用它:

DSN=mn_test; Uid=postgres; Pwd=postgres;

然后你可以使用其他形式的连接字符串:

Driver={PostgreSQL UNICODE};Server=127.0.0.1; Port=5493; Database=mn_test; Uid=postgres; Pwd=postgres;

两者都适用于我的旧 32 位 Windows 环境。我用简单的 Python 脚本测试它们(我使用 ActiveState Python,其中有简单的odbc模块):

import odbc

def test_odbc(connect_string):
    print(connect_string)
    db = odbc.odbc(connect_string)
    c = db.cursor()
    rs = c.execute("SELECT version()")
    for txt in c.fetchall():
        print('%s' % (txt[0]))
    print('-----')

test_odbc('Driver={PostgreSQL UNICODE};Server=127.0.0.1; Port=5493; Database=mn_test; Uid=postgres; Pwd=postgres;')
test_odbc('DSN=mn_test; Uid=postgres; Pwd=postgres;')
于 2016-01-14T11:01:17.410 回答