views:

164

answers:

2

Well, I hope this is not a duplicate, the search did not yield anything useful.

I have been toying with cx_Oracle for the past few days, installing and using it. Everything went fine until I reached my current problem: I'd like to change my schema. If I were using sqlplus a simple 'alter session set current_schema=toto;' would do, but I don't know how to get around it with cx_Oracle.

I've downloaded the latest source version: cx_Oracle-5.0.2.tar.gz.

According to the documentation changing of schema is a simple case of setting Connection.current_schema which should be a read-write attribute... the trouble is my Connection object does not have any current_schema attribute.

>>> c = cx_Oracle.connect(...)
>>> dir(c)
['__class__', '__delattr__', '__doc__', '__enter__', '__exit__', '__format__', 
'__getattribute__', '__hash__', '__init__', '__new__', '__reduce__',
'__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', 
'__subclasshook__', 'autocommit', 'begin', 'cancel', 'changepassword', 'close', 
'commit', 'cursor', 'dsn', 'encoding', 'inputtypehandler',
'maxBytesPerCharacter', 'nencoding', 'outputtypehandler', 'password', 'prepare', 
'register', 'rollback', 'stmtcachesize', 'tnsentry', 'unregister', 'username', 
'version']

Trying to set the attribute using

>>> c.current_schema = 'toto'

results in an error... __setattr__ has apparently been overridden to prevent it.

So... does anyone know how to ?


Here is the error I got.

>>> c.current_schema = 'toto'
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
AttributeError: 'cx_Oracle.Connection' object has no attribute 'current_schema'

>>> setattr(c, 'current_schema', 'toto')
# same error

And here are the information about OS and python:

SUSE LINUX Enterprise Server 9 (x86_64)
VERSION = 9
PATCHLEVEL = 3

And I use python 2.6.2 (compiled for 64bits)

I also compiled cx_Oracle for 64bits, on the very same machine.

+1  A: 

Try reinstalling cx_Oracle. Your cx_Oracle is probably messed up. What's your OS and python version?

fn
I just installed it and there was no previous version before that could interfer...
Matthieu M.
+3  A: 

Okay, I finally, after much trying and error, followed fn suggestion and investigated inside cx_Oracle to find what was wrong.

It turns out that a number of arguments and methods are only available through some flags:

  • WITH_UNICODE activates encoding and nencoding attributes
  • ORACLE_10G activates action, module, clientinfo and current_schema

I checked and found out that I had compiled cx_Oracle against the version 9 of the oracle client... so I recompiled against the version 10.2.0.3 of the oracle client and now I have access to these attributes.

Shame that the restriction was not precised in the documentation... and I am very thankful that the source code is available.

Matthieu M.