473,379 Members | 1,542 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,379 software developers and data experts.

cx_Oracle and UTF8

Hello,

I am looking for a method to convince cx_Oracle and oracle to encode
it's replies in UTF8.

For the moment I have to...

cn=cx_Oracle.connect("user","password", "database")
cs=cn.Cursor()

cs.execute("select column1, column2, column3 from table")

for row in cs.fetchall():
t=[]
for i in range(0,len(row)):
if hasattr(row[i],"encode"):
t.append(row[i].encode("utf8"))
else:
t.append(row[i])
print t

Guess I am to much accustomed to postgresql which just allows "set
client_encoding='utf8'...

Harald

Feb 22 '06 #1
4 15369
Harald Armin Massa wrote:
Hello,

I am looking for a method to convince cx_Oracle and oracle to encode
it's replies in UTF8.

For the moment I have to...

cn=cx_Oracle.connect("user","password", "database")
cs=cn.Cursor()

cs.execute("select column1, column2, column3 from table")

for row in cs.fetchall():
t=[]
for i in range(0,len(row)):
if hasattr(row[i],"encode"):
t.append(row[i].encode("utf8"))
else:
t.append(row[i])
print t

Guess I am to much accustomed to postgresql which just allows "set
client_encoding='utf8'...


You can do that in Oracle, too. It's called NLS (national language support),
and it is like a locale-setting in python/C. I'm too lazy to google right
now, but you should be able to alter the current connection's session to
deliver strings in whatever encoding is supportde (utf-8 being amongst
these).

Diez
Feb 23 '06 #2
Dietz,

thank you for your answer.
It's called NLS (national language support),
and it is like a locale-setting in python/C. I'm too lazy to google right


Sad thing: I allready googled that and had to learn: you CAN definitely
change some parameters, that is sort order and language for error
messages with
alter session set NLSREGION and set NLSLANGUAGE

The only part about the charset is with NLSLANG, which could be set to
German_Germany.UTF8

BUT ... NLSLANG is no per-session parameter, not setable per alter
session, it needs to get set within the environment to make SQLPLUS
recognize it. (and, I do of course use python not sqlplus=

In another of the WWW I learned that NLSLANG has to be set on per
connection basis; not on per cursor / session basis; so my primary
suspect is cx_Oracle.Connection ... but those objects to not have a
visible method with any "encoding" in it.

Harald

Feb 23 '06 #3
Harald Armin Massa wrote:
Dietz,

thank you for your answer.
It's called NLS (national language support),
and it is like a locale-setting in python/C. I'm too lazy to google right


Sad thing: I allready googled that and had to learn: you CAN definitely
change some parameters, that is sort order and language for error
messages with
alter session set NLSREGION and set NLSLANGUAGE

The only part about the charset is with NLSLANG, which could be set to
German_Germany.UTF8

BUT ... NLSLANG is no per-session parameter, not setable per alter
session, it needs to get set within the environment to make SQLPLUS
recognize it. (and, I do of course use python not sqlplus= [...]


This is handled by the OCI, which both your Pyhton interface and SQLPLUS
use. You should be able to do something like:

import os
os.environ["NLS_LANG"] = "German_Germany.UTF8"
import cx_Oracle
con = cx_Oracle.connect("me/secret@tns")
cur = con.cursor()
cur.execute("select foo from bar")
print cur.fetchone()[0]

HTH,

-- Gerhard
Feb 23 '06 #4
Gerhard,

thanks, that

import os
os.environ["NLS_LANG"] = "German_Germany.UTF8"
import cx_Oracle
con = cx_Oracle.connect("me/secret@tns")

really helped. At least now the query returns something encoded
differently. I dared not to believe that there is no "direct encoding
change api" without touching the environment.

Now all that is left is to find out if Oracle indeed has the same
opinion what UTF8 should be like.

Thank you very much,

Harald

Feb 27 '06 #5

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: infidel | last post by:
I have a stored procedure that has a single output parameter. Why do I have to pass it a string big enough to hold the value it is to receive? Why can't I pass an empty string or None? >>>...
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel

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.