any cx_Oracle sample code?

M

Mike Stenzler

Does anyone have any sample code for the cx_Oracle library?
www.computronix.com

I have the doco and can connect and do simple ops against my database but
I'm struggling with
the correct syntax for implementing bind variables with a cursor.


TIA

Mike
 
M

Mike Stenzler

Mike Stenzler said:
Does anyone have any sample code for the cx_Oracle library?
www.computronix.com

I have the doco and can connect and do simple ops against my database but
I'm struggling with
the correct syntax for implementing bind variables with a cursor.


Nevermind...

Some diligent reading of the python DB-API 2.0 yielded the required
knowledge..

import cx_Oracle

## execute wants it's parameters in a dictionary
params = {'val_1':"AA"}

## sql statement uses a bind var style of "named" as default - ":" denotes
bind var
sqlstr = "select name from my.table where id = :val_1"

## open a connection
conn = cx_Oracle.Connection("user/pword@server")

## open a cursor
curs = conn.cursor()

## tell Oracle how many rows to fetch
## at a time - default is 1
curs.arraysize = 256

## send the sql statement to Oracle parser for execution
curs.execute(sqlstr, params)

## fetch resultset from cursor
for name in curs.fetchall():
print "name: ", name

mike
 
S

Stefan Eischet

Hi Mike,

you can also just use keyword args like

curs.execute("""\
SELECT *
FROM table
WHERE table.field1 = :keyword1
AND table.field2 = :keyword2
""", keyword1="value1", keyword2=17)

instead of passing a dict.

I'm curious: why do you set the arraysize and then do a .fetchall? As I
understand it, fetchall does not look at arraysize, only fetchmany
does, in case you don't supply a size in the fetchmany call (i.e.
curs.fetchmany(15)); did I miss something?

Cheers,
Stefan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top