Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing schema using cx_Oracle

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.

like image 618
Matthieu M. Avatar asked Jan 06 '10 09:01

Matthieu M.


People also ask

How do I change the schema of a session in Oracle?

The following statement sets the schema of the current session to the schema name specified in the statement. ALTER SESSION SET CURRENT_SCHEMA = < schema name > In subsequent SQL statements, Oracle Database uses this schema name as the schema qualifier when the qualifier is omitted.

What is a schema qualifier in Oracle?

In subsequent SQL statements, Oracle Database uses this schema name as the schema qualifier when the qualifier is omitted. In addition, the database uses the temporary tablespace of the specified schema for sorts, joins, and storage of temporary database objects.

How do I enable industry-specific schemas in Oracle CX audience?

You have the option of enabling industry-specific schemas in your Oracle CX Audience account. When you enable this option, CX Audience does the following: Create new tables for the schema. For the retail schema, these tables are Orders, Order line, and Product lookup. Learn more about The Retail predefined schema.

What is a schema change plan?

Schema Change Plans—A means of deploying specific changes from a development environment to one or more target databases. Data Comparison—A list of differences in row data between two databases.


2 Answers

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.

like image 168
Matthieu M. Avatar answered Sep 25 '22 02:09

Matthieu M.


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

like image 43
fn. Avatar answered Sep 22 '22 02:09

fn.