All Forums Connectivity
MikeC 11 posts Joined 03/09
16 Sep 2009
Connecting Python to Teradata over ODBC

Has anyone ever connected a Python based script to Teradata using the ODBC driver on Linux? We have a Sales Consultant trying to work with a prospect and getting seg faults while using the ODBC driver.
ODBC connect string for Teradata Express 12.0:
DRIVER={Teradata};DBCNAME=localhost;UID=dbc;PWD=dbc; QUIETMODE=YES;
Example interactive Python session: session: >>> conn = >>> pyodbc.connect('DRIVER={Teradata};DBCNAME=localhost; UID=dbc;PWD=dbc; >>> QUIETMODE=YES;') >>> conn
>>> curs = conn.cursor() >>> curs.execute('select current_timestamp')
>>> curs.fetchone() (datetime.datetime(2009, 8, 13, 11, 34, 1, 250), )

MikeC (for jpm185170)

Tags:
Jimm 165 posts Joined 09/07
17 Sep 2009

The example you gave is from a pyodbc website.
If you "import pyodbc" before running this example, it works fine (at least on Windows)
See below for example.

Can you give me any details on the actual error you are getting?

>>> import pyodbc
>>> conn=pyodbc.connect('DRIVER={Teradata}; DBCNAME=localhost;UID=dbc;PWD=dbc;QUIETMODE=YES;')
>>> conn

>>> curs=conn.cursor()
>>> curs.execute('select current_timestamp')

>>> curs.fetchone()
(datetime.datetime(2009, 9, 17, 10, 40, 8, 870), )
>>>

jpm185170 2 posts Joined 09/09
17 Sep 2009

You are correct - that is where the example came from. pyodbc was imported before getting the seg fault. Driver manager is unixODBC on 64 bit, pyodbc is also built on 64 bit. unixODBC's isql tool and the DataDirect driver can connect as long as I specify uid and pwd on the command line -- e.g. #isql testdsn dbc dbc works fine, #isql testdsn returns with error inavlid username,password, or account string - even though they are specified in the dsn. Windows connectivity is fine no problems. I also tried DataDirect's ddtestlib on pyodbc.so to see if it would load ok. Error was "undefined symbol: _Py_TrueStruct". This might have passed me by in the build process and not sure how this might affect connectivity.

The seg faults do not allow any odbc tracing logs or additional information. I've also tried an strace and ltrace, but nothing I can glean from that either.

jpm185170 2 posts Joined 09/09
28 Sep 2009

Resolution
Once we verified the connection was successful by viewing the ODBC Trace, I called in Vittal to help understand why the isql was encountering a SEGV after the connection. We will investigate this failure later as it might be a problem with the ODBC Driver. Since it was verified that the connection was successful we decided to move ahead and try the actual program python. There was an initial problem identified by Vittal that needed to be corrected by adding the defines below and a SQLSetEnvAttr call to set the SQL_ATTR_APP_UNICODE_TYPE attribute in a python module. Recompile pyodbc using "python setup.py build install".

Defines
#define SQL_CONOPT_START 1040
#define SQL_ATTR_APP_UNICODE_TYPE (SQL_CONOPT_START+24)
#define SQL_DD_CP_ANSI 0
#define SQL_DD_CP_UCS2 1
#define SQL_DD_CP_UTF8 2
#define SQL_DD_CP_UTF16 SQL_DD_CP_UCS2

And added the API call to set the SQL_ATTR_APP_UNICODE_TYPE attribute.

odbc_ret_code = SQLSetEnvAttr(henv, SQL_ATTR_APP_UNICODE_TYPE,
(void *) SQL_DD_CP_UTF16, SQL_IS_INTEGER);

You must sign in to leave a comment.