TimesTen database connection issue in python - python

I have 2 machines setup:
RDP terminal with timesten client 11.2.1 and pyodbc==3.0.7 and works great. I use this connection string:
cnxn = pyodbc.connect('DSN=myhostip;UID=myusername;PWD=mypass')
local machine with timesten client 11.2.2 and pyodbc==4.0.22 (I've also tried older versions, like 3.0.7)
and when I use the connection string:
cnxn = pyodbc.connect('DSN=myhostip1;UID=myusername1;PWD=mypass1')
I get this error:
pyodbc.Error: ('HY010', u'[HY010] [Microsoft][ODBC Driver Manager] The driver is incapable of supporting the current environment attributes. (0) (SQLDriverConnect)').
Both databases on both machines are practically the same, so difference is only in timesten driver version.
Or what else could be the problem?
I have set up DSNs on both machines, and I connect to them in SQLDeveloper.
I've tried different timesten clients and pyodbc versions, but got same error everytime.
It's the only way that I know of to connect to timesten DB, can anyone please help me solve this error, or maybe tell about another way,except pyodbc, to connect?

See this answer on how to connect to TimesTen via Python:
python access to TimesTen
Use cx_Oracle via tnsnames.ora as this is the method that Oracle will support in TimesTen 18.1.3
Please avoid using any ODBC based method to connect to Python as none of these techniques are developed or tested by Oracle.

Related

pyodbc fails to connect to database, but IBM data studio connects with same credentials

I'm trying to connect to a database on an IBM machine, and I can connect just fine via the IBM desktop client "IBM Data Studio." However, when I try to connect with pyodbc it fails to connect. I've received a series of errors but it seems the main response is along the lines of the following
pyodbc.OperationalError: ('08001', u'[08001] [Microsoft][ODBC SQL Server Driver]
[TCP/IP Sockets]SQL Server does not exist or access denied. (17)
(SQLDriverConnect); [08001] [Microsoft][ODBC SQL Server Driver][TCP/IP Sockets]ConnectionOpen (
Connect()). (10061); [08001] [Microsoft][ODBC SQL Server Driver]Invalid connection string attribute (0)')
My code follows:
import pyodbc
# Specifying the ODBC driver, server name, database, etc. directly
cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER=*****; PORT=50000;DATABASE=****;UID=***;PWD=***')
# Create a cursor from the connection
cursor = cnxn.cursor()
cnxn.close()
I am working on a Windows machine.
You cannot use an ODBC Driver for Microsoft SQL Server, to connect to an instance of IBM DB2 (nor to any DBMS other than Microsoft SQL Server).
You need an ODBC Driver for IBM DB2 for this connection, such as those from my employer.
You could also use an ODBC-to-JDBC Bridge Driver, in combination with a JDBC Driver for IBM DB2, such as JTOpen (open source, from IBM).
Just because a jdbc connection from Data-Studio is working, it does not mean that your pyodbc will connect to Db2. pyodbc does not use jdbc, instead it will use the CLI/ODBC interface to Db2 which gets implemented by a suitable driver.
To use Db2 from pyodbc on Windows, ensure you first have either a Db2-client installed on Windows, or a Db2-server installed installed on Windows.
There are many kinds of Db2-clients. Much depends on the operating-system that runs your Db2-server (Z/OS, i-Series, Linux, Unix, Windows), and what kind of activities you want to perform on the client (developing, administering/monitoring, querying, or all three).
For some target platforms there are non-IBM drivers, but I won't discuss those.
In python, you can choose to connect to a database either with a DSN (Data Source Name) (usually this involves a shorter connection string)
or without a DSN (longer connection string containing all the details).
When learning, it may be easier to get Microsoft Windows to do most of the initial work with odbcad32.
This is most easy if the Db2-client is already configured to access one or more Db2-databases . The prereq is that the driver supports CLI/ODBC.
To define a DSN (either a system-DSN or a user-DSN) use the Microsoft odbcad32 tool to point to your Db2 database and verify connectivity.
If the Db2-server runs on Z/OS or i-Series, special licensing requirements may apply depending on whether you are directly connecting to the target Db2-server or whether you are using a Db2-connect gateway.
Take a note of the exact DRIVER string inside odbcad32 for the Db2-database, including case and spaces because you will need that in your python code.
For example, that driver name might look like 'IBM DB2ODBC DRIVER - DB2COPY1' if you have a local Db2-server installed on Windows (such as the free Db2-Express-C).
Verify that the connection to the Db2-database is successful inside odbcad32. That is crucial.
When odbcad32 succeeds to connect then pyodbc will succeed to connect usually.
In your python code, your connection-string can either use the DSN or explicitly quote the DRIVER/SERVER/PORT/DATABASE/UID/PWD, along with any other required settings on the connection string.
Remember also that you don't have to use pyodbc. There are other options for python to interact with Db2-Servers. Make an informed choice and do your research.
You can also use the IBM supplied module "ibm_db" or the DBI interface module "ibm_db_api", or if you are using an object relational mapper you can use the SQLAlchemy adapter (ibm_db_sa), or you can use django framework.
Read all about that in the Db2 documentation here.
https://www.ibm.com/support/knowledgecenter/en/SSEPGG_10.5.0/com.ibm.swg.im.dbclient.python.doc/doc/c0054366.html
I have solved the problem! The issue was that I was missing the "iSeries Access ODBC Driver". I don't think it's available online, although I could be wrong, I had to have our database guy help install it. Now I can connect through both the ibm_db and pyodbc libraries, after setting up my DSN under "odbcad32.exe." This took a while to solve but mostly because of the lack of informative documentation from IBM. Hopefully, this helps anyone in the same situation.

How should I configure the a connection to MS Azure cloud database using pyodbc?

I'm setting up a python script to connect to an instance of the azure cloud database, and seeing a difference in the connection when I use IDLE vs when I try to execute the connection from a script. For example, when I do the following in IDLE:
>>> import pyodbc
>>> conn = pyodbc.connect('DRIVER={SQL Server};SERVER=<my.database.host.info>,1433', user='<my_username#mydatabase', password='password', database='database')
I'm able connect successfully and execute a basic query against one of the tables in the database.
However when I use the same connection string (copied from the IDLE console) in a script, I get the following error:
pyodbc.Error: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')
Usually, the issue is raised because the 32/64 bit version of python environment and odbc driver are mismatched.
You may check the python version and odbc driver version you used to run the script. You have to manage to change both of them need to be 32-bit (or both 64-bit) in order for this to work.
You can get the specified version of pyodbc extension from http://www.lfd.uci.edu/~gohlke/pythonlibs/#pyodbc.

Error in ODBC but not in CLI for Hive?

I am using pyodbc to query Hive. I also have access to a CLI for entering queries. When I enter the query in CLI, everything runs, but when I enter it in pydobc
cursor.execute(query)
results = cursor.fetchall()
I get this error:
Error: ('HY000', "[HY000] [Cloudera][HiveODBC] (35) Error from Hive: error code: '0' error message: 'java.io.IOException: java.io.EOFException'. (35) (SQLFetch)")
Sometimes it runs fine without giving this error, and sometimes it fails. I am at a loss as to what is causing this. It only happens when I select a subset of columns, not SELECT *.
The message from ODBC shows the "java.io.IOException: java.io.EOFException" which is an exception thrown in a Java stack. The error is very likely not originated from within the ODBC driver but in the server.
The ODBC driver communicates with Hive through the Thrift server (Hive Server 1 or Hive Server 2) but CLI by-passes the Thrift server. Have you try using beeline to reproduce this issue? Beeline uses the open-source JDBC driver to communicate with Hive through the Thrift server.
If this issue is also reproducible with beeline then the error might be originated from the Thrift server while fetching the results. In that case you may want to check the server logs for more details about the error. If you are using CDH you can check the HiveServer2 log in Cloudera Manager (I am guessing you are likely using Hive Server 2).
Cheers,
Holman

pyodbc error: Data source name not found (Win8)

I am trying to connect using the pyodbc capability with the following connection string:
DRIVER={SQL Native Client}.
Getting the following error:
Error connecting to database: [IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)
Main issue is that on my machine everything works like a charm, while execution on other machines return the error above.
Many thanks!
That is presumably because your connection string specifies some Windows datasource (a datasource is effectively an os-level alias) which exists on your machine, but does not exist on the other machines. You probably need to define the datasource on the other machines.
As Flipper suggests, first check out your connection string to determine what the name of the datasource is. Then check on your machine how that datasource is configured, then create and configure similar datasources on the other machines.
maybe check out the info and links on this ms dev-net page on datasources:
ODBC Data Source Administrator
All Windows installs include a copy of the older "SQL Server" ODBC driver
Driver={SQL Server}
so that should be available on any Windows machine for both 32-bit and 64-bit applications.
More recent versions of SQL Server have introduced their own client software to support their latest features. Drivers like
Driver={SQL Native Client} (9.0, for SQL Server 2005)
Driver={SQL Server Native Client 10.0} (for SQL Server 2008)
Driver={SQL Server Native Client 11.0} (for SQL Server 2012/2014)
will only work if the machine has the required SQL Server client software installed. (It can be downloaded and installed separately for machines that don't run SQL Server itself, usually as part of a "SQL Server Feature Pack".)
So, if you really need the more advanced features of the later "Native Client" ODBC driver then you will have to ensure that it gets installed on the other machines. Otherwise, just stick with the older "SQL Server" driver.

Using SQL Server Native Client With Pyodbc on Linux

I'm trying to use Python on a RedHat machine to connect to a SQL Server database using the Microsoft provided driver. I know the driver is working because I can connect using the sqlcmd. The following in Python yields an error. It seems that pyodbc can't find the driver. Anybody know how to fix this?
conStr = 'Driver={SQL Server Native Client 11.0};Server='+server+';Database='+db+';
UID='+u+';PWD='+pw+';'
cnxn = pyodbc.connect(conStr)
pyodbc.Error: ('IM002', '[IM002] [unixODBC][Driver Manager]Data source name not found, and no default driver specified (0) (SQLDriverConnect)')
I found that on Linux I needed to remove the braces around the driver name.
Some more info on the Driver keyword can be gleaned from here:
http://msdn.microsoft.com/en-us/library/hh568455
Actually, You can use pymssql to connect SQLServer in python.
It's easy to use, similar with MySQL-python lib.
http://code.google.com/p/pymssql/
Since pyodbc is going through unixODBC, can you confirm odbc.ini and odbcinst.ini are correctly set up?

Categories

Resources