python & oracle python & oracle requirements o oracle instant client o cx_oracle module ...

6
Python & Oracle • Requirements o Oracle Instant Client o cx_Oracle module http://cx-oracle.sourceforge.net / • Installation o Windows: Win Installer o Linux: RPM or cx_Oracle.so

Upload: florence-carson

Post on 16-Jan-2016

229 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Python & Oracle Python & Oracle Requirements o Oracle Instant Client o cx_Oracle module  Installation o Windows: Win Installer

Python & Oracle

• Requirementso Oracle Instant Cliento cx_Oracle module http://cx-oracle.sourceforge.net/

• Installationo Windows: Win Installero Linux: RPM or cx_Oracle.so

Page 2: Python & Oracle Python & Oracle Requirements o Oracle Instant Client o cx_Oracle module  Installation o Windows: Win Installer

Example: accessing database

import cx_Oracle

connection = cx_Oracle.connect('username/password@localhost')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()

Page 3: Python & Oracle Python & Oracle Requirements o Oracle Instant Client o cx_Oracle module  Installation o Windows: Win Installer

Example: connection pooling

import cx_Oracle

pool = cx_Oracle.SessionPool(USER, PASSWORD, TNS, 1, #min number of sessions controlled by pool3, #max number of sessions controlled by pool1, #additional sessions to be opened per acquireDB.Connection, #connection typeTrue) #OCI_THREADED

pool.timeout = 120 #idle session timeout

#thread bodyfirst='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 bodypool.release(connection)

Page 4: Python & Oracle Python & Oracle Requirements o Oracle Instant Client o cx_Oracle module  Installation o Windows: Win Installer

Connection API• Functions

o begin() - explicitly begin a new transactiono cancel() - cancel a long-running transactiono close() - close connectiono commit() - commit any pending transactions to the databaseo cursor() - returns new cursor objectso ping() - tests if the connection is still activeo rollback() - rollback any pending transactions

• Attributeso autocommit - read-write, autocommit mode is on or offo current_schema - read-write, sets the current schema for the sessiono password - read-writeo encoding - read-only, character set in use by the

Oracle cliento stmtcachesize - read-write, specifies the size of the statement

cacheo tnsentry - read-only, returns the TNS entry of the databaseo username - read onlyo version - read-only, version of the database

Page 5: Python & Oracle Python & Oracle Requirements o Oracle Instant Client o cx_Oracle module  Installation o Windows: Win Installer

Cursor API• Functions

o callfunc(name, returnType, parameters=[], keywordParameters = {})o callproc(name, parameters=[], keyewordParameters = {})o connection() - returns a reference to the connection objecto bindnames() - return list of bind variable nameso execute(statement[, parameters], **keywordParameters)o executemany(statement, parameters) o fetchall()o fetchmany([numRows=cursor.arraysize])o fetchone() - fetching of next rowo next() - like fetchone()o parse(statement) - does parsing onlyo prepare(statement[, tag]) – does preparation of the statemento close()

• Attributeso arraysize -  read-write, number of rows to fetcho bindvars - read-onle, bind variables used for the last executeo rowcount - read-only, number of rows fatched

Page 6: Python & Oracle Python & Oracle Requirements o Oracle Instant Client o cx_Oracle module  Installation o Windows: Win Installer