Re: [cx-oracle-users] maintaining multiple connections with different encodings
Brought to you by:
atuining
From: Anthony T. <ant...@gm...> - 2011-05-09 16:14:44
|
Hi, First, Oracle does not allow redefining the default encoding with multiple connections. So the behavior you are seeing is expected. Second, why do you want to use different encodings? Oracle will automatically translate between whatever encoding is used in the database and whatever encoding you are using on the client anyway. So you can feel quite free to use utf-8 for both databases and everything will work just fine since utf-8 is capable of displaying all of the characters in the latin-1 character set. Hope that helps. Anthony On Mon, May 9, 2011 at 7:00 AM, Dana Pieluszczak <da...@gm...> wrote: > I have a python process which needs to connect to multiple oracle > databases with different NLS_CHARACTERSET values. > > I can set the appropriate character set by setting the NLS_LANG > environment variable, either in my shell or using os.environ before > connecting. For utf-8 databases, I set it to "AMERICAN_AMERICA.UTF8", > for Latin-1 (ISO-8859-1) I set it to "AMERICAN_AMERICA.WE8ISO8859P1". > > Here's some code that illustrates the problem: > > import os > import cx_Oracle > > os.environ['NLS_LANG'] = 'AMERICAN_AMERICA.UTF8' > utf8_conn = cx_Oracle.connect('dana', 'pw', 'utf8db') > print utf8_conn.encoding > s = utf8_conn.cursor().execute('SELECT :s AS str FROM dual', > s=u'd\u00e4t\u00e4').fetchone()[0] > print s, type(s), len(s), len(s.encode('utf8')) > > utf8_conn.close() > > os.environ['NLS_LANG'] ='AMERICAN_AMERICA.WE8ISO8859P1' > latin1_conn = cx_Oracle.connect('dana', 'pw', 'latin1db') > print latin1_conn.encoding > s = latin1_conn.cursor().execute('SELECT :s AS str FROM dual', > s='d\xe4t\xe4').fetchone()[0].decode('latin1') > print s, type(s), len(s), len(s.encode('latin1')) > > This outputs: > UTF-8 > dätä <type 'unicode'> 4 6 > ISO-8859-1 > dätä <type 'unicode'> 4 4 > > The problem is, if I don't close the utf8 connection before opening > the latin1 connection, the latin1 connection's encoding will be utf8. > Here's the output with the utf8_conn.close() call removed: > > UTF-8 > dätä <type 'unicode'> 4 6 > UTF-8 > d¿ <type 'unicode'> 3 3 > > Is there any way around this? I'd like to be able to open all my > database connections at startup time instead of constantly > opening/closing them. > > I'm using cx_Oracle 5.1 with oracle client version 10.2.0.1 on SLES > 10. Both databases that I'm connecting to are 10.2.0.5. > > Thanks, > > Dana P > > ------------------------------------------------------------------------------ > WhatsUp Gold - Download Free Network Management Software > The most intuitive, comprehensive, and cost-effective network > management toolset available today. Delivers lowest initial > acquisition cost and overall TCO of any competing solution. > http://p.sf.net/sfu/whatsupgold-sd > _______________________________________________ > cx-oracle-users mailing list > cx-...@li... > https://lists.sourceforge.net/lists/listinfo/cx-oracle-users > |