Additional Information for Python

This section covers these topics:

Type Mappings for Python Applications

There are mappings between python-oracledb types (as available through the language extension module) and TimesTen SQL types.

Table 3-1 documents mappings between python-oracledb types (as available through the language extension module) and TimesTen SQL types.

Note:

Additional TimesTen SQL types can be mapped to these python-oracledb types as a result of TimesTen implicit data type conversions. See Data Type Conversion in Oracle TimesTen In-Memory Database SQL Reference.

For the latest information about data type support in the current release, refer to the README.md file at TimesTen Python Samples.

Table 3-1 Type Mappings for python-oracledb

python-oracledb Type TimesTen Type

oracledb.DB_TYPE_BINARY_DOUBLE

BINARY_DOUBLE

oracledb.DB_TYPE_BINARY_FLOAT

BINARY_FLOAT

oracledb.DB_TYPE_BLOB

BLOB

oracledb.DB_TYPE_CHAR

CHAR

oracledb.DB_TYPE_CLOB

CLOB

oracledb.DB_TYPE_CURSOR

REF CURSOR

oracledb.DB_TYPE_DATE

DATE

oracledb.DB_TYPE_NCHAR

NCHAR (In TimesTen, this type is UTF-16 only.)

oracledb.DB_TYPE_NCLOB

NCLOB

oracledb.DB_TYPE_NUMBER

NUMBER, TT_BIGINT, TT_INTEGER, TT_SMALLINT, TT_TINYINT

oracledb.DB_TYPE_NVARCHAR

NVARCHAR2 (In TimesTen, this type is UTF-16 only.)

oracledb.DB_TYPE_RAW

BINARY, VARBINARY

oracledb.DB_TYPE_ROWID

ROWID

oracledb.DB_TYPE_TIMESTAMP

TIMESTAMP, TT_TIMESTAMP

oracledb.DB_TYPE_VARCHAR

VARCHAR2

Failure Modes for Python

There are several failure modes for Python.

Be aware of the following:

  • Error messages may differ slightly from those produced by Oracle under the same conditions.

  • For the Python method Cursor.executemany(), TimesTen ignores the batcherrors option and therefore supports only the default batcherrors=false setting. An error is always returned if any row in the batch encounters an error. (By contrast, Oracle Database supports batcherrors=true, in which case success is always returned if any row in the batch is successful.) Also, against a TimesTen database, if multiple errors are encountered in a batch, TimesTen makes no attempt to order the errors as they would be ordered if encountered against an Oracle database.

Python Sample: Connect to TimesTen and Execute SQL

This sample program does the following:

  • Connects to a TimesTen database.

  • Creates a table named employees.

  • Inserts three rows into the table.

  • Selects and displays the rows.

  • Drops the table.

  • Disconnects from the database.

import oracledb

def run():
  try:
    oracledb.init_oracle_client();
    connection = oracledb.connect(
        user='appuser', 
        password='password', 
        dsn='localhost/sampledb:timesten_direct')
    cursor = connection.cursor()
    cursor.execute('''
        CREATE TABLE employees(first_name VARCHAR2(20), last_name VARCHAR2(20))''')
    print('Table created')
    values = [['ROBERT', 'ROBERTSON'], ['ANDY', 'ANDREWS'], ['MICHAEL', 'MICHAELSON']]
    cursor.executemany('INSERT INTO employees VALUES (:1, :2)', values)
    print('Inserted', len(values), 'employees into the table')
    cursor.execute('''
        SELECT first_name, last_name FROM employees''')
    for fname, lname in cursor:
      print('Selected employee:', fname, lname)
    cursor.execute('DROP TABLE employees')
    print('Table dropped')
    cursor.close()
    connection.close()
    print('Connection closed')

  except Exception as e:
    print('An error occurred', str(e))

run()

Running this sample program to connect to a TimesTen database should return the following output:

Table created
Inserted 3 employees into the table
Selected employee: ROBERT ROBERTSON
Selected employee: ANDY ANDREWS
Selected employee: MICHAEL MICHAELSON
Table dropped
Connection closed

See Task 4: Configure Connections for TimesTen for information on the Easy Connect method used to connect to the database in the sample program.

Python Sample Programs for Download

TimesTen provides sample programs for Python through the python-oracledb driver. These sample programs are available at TimesTen Python Samples.