Presentation is loading. Please wait.

Presentation is loading. Please wait.

Python & Oracle Requirements Installation Oracle Instant Client

Similar presentations


Presentation on theme: "Python & Oracle Requirements Installation Oracle Instant Client"— Presentation transcript:

1 Python & Oracle Requirements Installation Oracle Instant Client
cx_Oracle module Installation Windows: Win Installer Linux: RPM or cx_Oracle.so

2 Example: accessing database
import cx_Oracle connection = cursor = connection.cursor() bind_vars={'uid':25} sql='SELECT id,Firstname,Lastname FROM TB_NAME where id>:uid‘ cursor.execute( sql, bind_vars) rows = cursor.fetchall() for id,firstname,lastname in rows: print str(id)+' '+firstname+' '+lastname+"\n“ cursor.close() connection.close()

3 Example: connection pooling
import cx_Oracle pool = cx_Oracle.SessionPool( USER, PASSWORD, TNS, 1, #min number of sessions controlled by pool 3, #max number of sessions controlled by pool 1, #additional sessions to be opened per acquire DB.Connection, #connection type True) #OCI_THREADED pool.timeout = 120 #idle session timeout #thread body first='Cristiano' lase='Ronaldo' connection = pool.acquire() cursor = connection.cursor() cursor.execute("insert into players values (:a,:b)", {'a':first,'b':last} ) connection.commit() cursor.close() #end of thread body pool.release(connection)

4 Connection API Functions Attributes
begin() - explicitly begin a new transaction cancel() - cancel a long-running transaction close() - close connection commit() - commit any pending transactions to the database cursor() - returns new cursor objects ping() - tests if the connection is still active rollback() - rollback any pending transactions Attributes autocommit - read-write, autocommit mode is on or off current_schema - read-write, sets the current schema for the session password - read-write encoding - read-only, character set in use by the Oracle client stmtcachesize - read-write, specifies the size of the statement cache tnsentry - read-only, returns the TNS entry of the database username - read only version - read-only, version of the database

5 Cursor API Functions Attributes
callfunc(name, returnType, parameters=[], keywordParameters = {}) callproc(name, parameters=[], keyewordParameters = {}) connection() - returns a reference to the connection object bindnames() - return list of bind variable names execute(statement[, parameters], **keywordParameters) executemany(statement, parameters) fetchall() fetchmany([numRows=cursor.arraysize]) fetchone() - fetching of next row next() - like fetchone() parse(statement) - does parsing only prepare(statement[, tag]) – does preparation of the statement close() Attributes arraysize -  read-write, number of rows to fetch bindvars - read-onle, bind variables used for the last execute rowcount - read-only, number of rows fatched

6


Download ppt "Python & Oracle Requirements Installation Oracle Instant Client"

Similar presentations


Ads by Google