cx_Oracle and UTF8

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Harald Armin  Massa

    #1

    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.co nnect("user","p assword", "database")
    cs=cn.Cursor()

    cs.execute("sel ect 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

  • Diez B. Roggisch

    #2
    Re: cx_Oracle and UTF8

    Harald Armin Massa wrote:
    [color=blue]
    > 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.co nnect("user","p assword", "database")
    > cs=cn.Cursor()
    >
    > cs.execute("sel ect 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'...[/color]

    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

    Comment

    • Harald Armin  Massa

      #3
      Re: cx_Oracle and UTF8

      Dietz,

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

      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.Conne ction ... but those objects to not have a
      visible method with any "encoding" in it.

      Harald

      Comment

      • Gerhard Häring

        #4
        Re: cx_Oracle and UTF8

        Harald Armin Massa wrote:[color=blue]
        > Dietz,
        >
        > thank you for your answer.
        >[color=green]
        >>It's called NLS (national language support),
        >>and it is like a locale-setting in python/C. I'm too lazy to google right[/color]
        >
        > 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= [...][/color]

        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.conne ct("me/secret@tns")
        cur = con.cursor()
        cur.execute("se lect foo from bar")
        print cur.fetchone()[0]

        HTH,

        -- Gerhard

        Comment

        • Harald Armin  Massa

          #5
          Re: cx_Oracle and UTF8

          Gerhard,

          thanks, that

          import os
          os.environ["NLS_LANG"] = "German_Germany .UTF8"
          import cx_Oracle
          con = cx_Oracle.conne ct("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

          Comment

          Working...