python access to TimesTen

2020-03-27 17:16发布

问题:

I googled a lot to find any python module to access TimesTen (in memory database). I am writing a automated testing framework (more like for System Test and not Unit Test). Is anyone aware of such module? The last resort is write the wrapper myself but this is something I really want to avoid.

回答1:

To use Python with TimesTen (11.2 or 18.1), you should use cx_Oracle

cx_Oracle works the same for both TimesTen and the Oracle RDBMS

cx_Oracle uses ODPI-C which is a C library wrapper to OCI

For both TimesTen and the Oracle RDBMS, cx_Oracle can either use the Easy connect method or tnsnames.ora in the connect string.

The following cx_Oracle example uses tnsnames.ora [ie doug/doug@sampledb]

# myscript.py

from __future__ import print_function

import cx_Oracle

connection = cx_Oracle.connect("doug", "doug", "sampledb")
cursor = connection.cursor()
cursor.execute("insert into t values (42)")
connection.commit()
connection.close()

The direct linked and client server tnsnames.ora entries for TimesTen 18.1.2.3 sampledb is:

sampledb =(DESCRIPTION=(CONNECT_DATA = (SERVICE_NAME = sampledb)(SERVER = timesten_direct)))

sampledbCS =(DESCRIPTION=(CONNECT_DATA = (SERVICE_NAME = sampledbCS)(SERVER = timesten_client)))

As always, you need to source bin/ttenv.sh to configure the environment!

On my Ubuntu 16.04 machine, my PATH and LD_LIBRARY_PATH were:

echo $PATH /home/ubuntu/tt18123/bin:/home/ubuntu/tt18123/install/bin:/home/ubuntu/tt18123/install/ttoracle_home/instantclient_11_2:/home/ubuntu/tt18123/install/ttoracle_home/instantclient_11_2/sdk:/home/ubuntu/.cargo/bin:/home/ubuntu/bin:/home/ubuntu/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

echo $LD_LIBRARY_PATH /home/ubuntu/tt18123/ttclasses/lib:/home/ubuntu/tt18123/install/lib:/home/ubuntu/tt18123/install/ttoracle_home/instantclient_11_2

echo $TNS_ADMIN /home/ubuntu/tt18123/install/network/admin/samples

Oracle TimesTen will officially support ODPI-C and cx_Oracle in TimesTen 18.1.3

Please avoid using any ODBC based python libraries with TimesTen as cx_Oracle is what Oracle Development tests and develops against.



回答2:

I didn't find native one, but TimesTen has ODBC interface that you could use.

http://www.compwisdom.com/topics/ODBC



回答3:

There is pyodbc which is supposed to work on odbc databases. I'm not sure if it'll actually work. I think pyodbc requires odbc 3, and timesten might not support that (haven't checked), but it is worth checking out.

There is also mxODBC, which I have not tried. That might work as well.

Right, so here's an update since I tried it out: (Assuming you're on some sort of unix) It actually works with pyodbc together with unixodbc. pyodbc requires ODBC3.x and TimesTen don't support that. But, unixodbc will "translate" between the two for you. That means you can't use the timesten libodbc.so from pyodbc, since that is missing a bunch of functions from ODBC3.



回答4:

The best way to access TimesTen via a Python module is using cx_Oracle.

cx_Oracle uses an OCI based driver. TimesTen supports OCI and you can connect via tnsnames.ora or the Easy Connect naming method like you would for an Oracle DB.



回答5:

@ScalableDBDoug, would you mind providing the python code snippet? I tried using cx_Oracle in python script but was getting error message:

cx_Oracle.DatabaseError: ORA-12547: TNS:lost contact

Here is my code snippet:


import cx_Oracle

dsn_tns = cx_Oracle.makedsn('TTSERVER',TTPORT,service_name='TTSERVICE');
print(dsn_tns)
connection = cx_Oracle.connect('USER', 'PASS', dsn_tns)

cursor = connection.cursor()
cursor.execute("""SELECT * FROM MYTABLE""")
for record in cursor:
    print("Values:", record)

As a workaround I'm using UnixODBC and pyodbc to query the timesten database for now, But would like to use the native cx_Oracle module to access timesten.

Thanks,