[英]Connect Python script to PostgreSQL database
Hey I'm not sure what is wrong with this connection string, i'm getting the error - pyodbc.Error: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)') 嘿,我不确定此连接字符串出了什么问题,我得到了错误-pyodbc.Error :('IM002','[IM002] [Microsoft] [ODBC驱动程序管理器]数据源名称未找到且没有默认值指定的驱动程序(0)(SQLDriverConnect)')
here is my script: 这是我的脚本:
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()
and here is my ODBCINST.INI file contents: 这是我的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
Any help would be appreciated! 任何帮助,将不胜感激!
check list of drivers by using pyodbc.drivers()
and use driver name from it 通过使用pyodbc.drivers()
检查驱动程序列表,并pyodbc.drivers()
使用驱动程序名称
the driver name should be like PostgreSQL ANSI(x64)
instead of PostgreSQL ANSI
. 驱动程序名称应类似于PostgreSQL ANSI(x64)
而不是PostgreSQL ANSI
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.