如何使用pyodbc连接Vertica
我看过关于 odbc.ini
的 iODBC 文档,还有 Vertica 文档。我也看到一个问题 有相同的错误,但是按照之前问题的回答,我还是没办法让连接字符串正常工作。
我现在有的内容:
/etc/odbcinst.ini
[HPVertica] Description = HP Vertica ODBC Driver Driver = /opt/vertica/lib64/libverticaodbc.so
/etc/odbc.ini
[ODBC Data Sources] VerticaDB1 = db1 database on HP Vertica [VerticaDB1] Description = db1 database on HP Vertica Driver = HPVertica Database = db1 Servername = 10.0.0.67 UID = dbadmin PWD = Port = 5433 Locale = en_GB [ODBC] Threading = 1
~/.odbc.ini
[DEFAULT] Driver = VerticaDB1
使用 isql
进行测试
[root@ip-10-0-0-67 /]# echo "select 1;" | isql -v VerticaDB1 +---------------------------------------+ | Connected! | | | | sql-statement | | help [tablename] | | quit | | | +---------------------------------------+ SQL> select 1; +---------------------+ | ?column? | +---------------------+ | 1 | +---------------------+ SQLRowCount returns 1 1 rows fetched
odbcinst -j
的输出是:
unixODBC 2.2.14 DRIVERS............: /etc/odbcinst.ini SYSTEM DATA SOURCES: /etc/odbc.ini FILE DATA SOURCES..: /etc/ODBCDataSources USER DATA SOURCES..: /root/.odbc.ini SQLULEN Size.......: 8 SQLLEN Size........: 8 SQLSETPOSIROW Size.: 8
在 Python 中使用 "VerticaDB1":
>>> import pyodbc >>> conn = pyodbc.connect("DRIVER={VerticaDB1};UID={dbadmin};PWD={...}") Traceback (most recent call last): File "", line 1, in pyodbc.Error: ('IM002', '[IM002] [unixODBC][Driver Manager]Data source name not found, and no default driver specified (0) (SQLDriverConnect)')
在 Python 中使用 "HPVertica":
>>> import pyodbc >>> conn = pyodbc.connect("DRIVER={HPVertica};UID={dbadmin};PWD={...}") Traceback (most recent call last): File "", line 1, in pyodbc.Error: ('HY000', '[HY000] [unixODBC][Vertica][ODBC] (10430) Not enough information provided to establish a connection to the data source and specified to not prompt for more information. (10430) (SQLDriverConnect)')
1 个回答
12
试着用 DSN
来连接:
conn = pyodbc.connect("DSN=VerticaDB1;UID=dbadmin;PWD=mypassword")
另外,你也可以用 DRIVER
来连接,但你需要提供更多的信息,比如数据库的类型、主机和端口:
conn = pyodbc.connect("DRIVER=HPVertica;SERVER=10.0.0.67;DATABASE=db1;PORT=5433;UID=dbadmin;PWD=mypassword")