【问题标题】:Connect Python script to PostgreSQL database将 Python 脚本连接到 PostgreSQL 数据库
【发布时间】:2016-03-25 03:02:41
【问题描述】:

嘿,我不确定这个连接字符串有什么问题,我收到错误 - pyodbc.Error: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found并且没有指定默认驱动程序 (0) (SQLDriverConnect)')

这是我的脚本:

import pyodbc
conn_str = (
    "DRIVER={PostgreSQL ANSI};"
    "DATABASE=postgres;"
    "UID=postgres;"
    "PWD=password;"
    "SERVER=localhost;"
    "PORT=5432;"
    )
conn = pyodbc.connect(conn_str)
crsr = conn.execute("SELECT * FROM initialdata limit 50")
row = crsr.fetchone()
print(row)
crsr.close()
conn.close()

这是我的 ODBCINST.INI 文件内容:

[PostgreSQL ANSI(x64) (32 bit)]
Driver=C:\Program Files\psqlODBC\0905\bin\psqlodbc30a.dll
Setup=C:\Program Files\psqlODBC\0905\bin\psqlodbc30a.dll
32Bit=1

[ODBC 32 bit Drivers]
PostgreSQL ANSI(x64) (32 bit)=Installed
PostgreSQL Unicode(x64) (32 bit)=Installed

[PostgreSQL Unicode(x64) (32 bit)]
Driver=C:\Program Files\psqlODBC\0905\bin\psqlodbc35w.dll
Setup=C:\Program Files\psqlODBC\0905\bin\psqlodbc35w.dll
32Bit=1

任何帮助将不胜感激!

【问题讨论】:

  • 根据 odbcinst.ini 文件,您的驱动程序名称应该是:PostgreSQL ANSI(x64) (32 bit) ...不仅仅是 PostgreSQL ANSI
  • ODBC 数据源管理器 (odbcad32.exe) 的驱动程序选项卡中的名称是什么样的。另外...看起来您在 64 位机器上运行 32 位驱动程序。你的 python 是 32 位还是 64 位?它应该与您安装的驱动程序相匹配。

标签: python postgresql odbc


【解决方案1】:

使用pyodbc.drivers()检查驱动程序列表并使用其中的驱动程序名称

驱动程序名称应该是PostgreSQL ANSI(x64) 而不是PostgreSQL ANSI

【讨论】:

    猜你喜欢
    • 2013-10-07
    • 1970-01-01
    • 1970-01-01
    • 2019-07-04
    • 1970-01-01
    • 1970-01-01
    • 2012-05-10
    • 2014-03-04
    • 1970-01-01
    相关资源
    最近更新 更多