473,480 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 6958

"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
8946
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
6249
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
8476
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
4542
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
4394
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
5256
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
14686
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
2580
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
4822
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...
0
7037
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7032
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7076
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6730
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
6873
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5321
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4767
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
2976
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
174
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.