473,378 Members | 1,609 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

any cx_Oracle sample code?

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

Jul 18 '05 #1
2 6952

"Mike Stenzler" <ms*******@ssaris.com> wrote in message
news:9r*******************@fe31.usenetserver.com.. .
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

Jul 18 '05 #2
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
On 22.07.2004, at 23:04, Mike Stenzler wrote:

"Mike Stenzler" <ms*******@ssaris.com> wrote in message
news:9r*******************@fe31.usenetserver.com.. .
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

--
http://mail.python.org/mailman/listinfo/python-list


Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: GrayGeek | last post by:
After cx_Oracle and the related Oracle tools for Python 2.2.3 + Boa-constructor on Win2000, I added "import cx_Oracle" to the top of a test script. It gives me an error about being unable to find...
8
by: Rodrigo Daunaravicius | last post by:
I can't get this configuration working: cx_Oracle 4.0.1 Oracle 8.0.6 Python 2.3.3 Win NT4.0 SP6fc My first shot at the problem was just running the installation thingie pre-built for...
1
by: Greg Lindstrom | last post by:
>Shouldn't this be '/u01/app/oracle/product/9.2.0' ? Based on the following values, that's what I'd expect. I changed the ORCALE_HOME environment variable to the above suggested value and got a...
1
by: jmdeschamps | last post by:
Hello Having cx_Oracle (an Oracle database connector for Python) used it here where I teach for the last couple of years, and finding it so easy to use (and install) I was taken aback when I got...
1
by: Maxim Kuleshov | last post by:
Hello! Trying to fetch long varchar2 column and get the following error: cx_Oracle.DatabaseError: column at array pos 0 fetched with error: 1406 i.e. string buffer is not much enough to...
4
by: MooMaster | last post by:
After some google searching on the forum I couldn't find any topics that seemed to relate exactly to my problem, so hopefully someone can help me out... I'm running python 2.4.1 on a local Win2K...
7
by: Carl K | last post by:
I am trying to use this: http://python.net/crew/atuining/cx_Oracle/html/cx_Oracle.html it is a real module, right? sudo easy_install cx_Oracle did not easy_install cx_Oracle. ...
1
by: Lukas Ziegler | last post by:
Hi, I want to get an access to an oracle database. For that I found the module cx_oracle (http://www.python.net/crew/atuining/cx_Oracle/) and I have installed the version 'Windows Installer...
3
by: Benjamin Hell | last post by:
Hi! I have a problem with the cx_Oracle module (Oracle database access): On a computer with cx_Oracle version 4.1 (Python 2.4.3, Oracle 10g) I can get query results consisting of strings...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.