简体   繁体   中英

Get SQLAlchemy to encode correctly strings with cx_Oracle

My problem is that SQLAlchemy seems to be writing the text not properly encoded in my Oracle database.

I include fragments of the code below:

engine = create_engine("oracle://%s:%s@%s:%s/%s?charset=utf8"%(db_username, db_password, db_hostname,db_port, db_database), encoding='utf8')    
connection = engine.connect()
session    = Session(bind = connection)    

class MyClass(DeclarativeBase):
    """
    Model of the be persisted
    """        
    __tablename__ = "enconding_test"

    id              = Column(Integer, Sequence('encoding_test_id_seq'),primary_key = True)    

    blabla  = Column(String(255, collation='utf-8'), default = '')           
    autoload = True    

content = unicode("äüößqwerty","utf_8")
t = MyClass(blabla=content.encode("utf_8"))

session.add(t)
session.commit()

If now I read the contents of the database, I get printed something like:

????????qwerty

instead of the original:

äüößqwerty

So basically my question is what do I have to do, to properly store these German characters in the database?

Thanks in advance!

I found a related topic, that actually answers my question:

Python 2.7 connection to oracle loosing polish characters

You simply add the following line, before creating the database connection:

os.environ["NLS_LANG"] = "GERMAN_GERMANY.UTF8"

Additional documentation about which strings you need for different languages are found at the Oracle website:

Oracle documentation on Unicode Support

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM