0

I am trying to insert dataframe to oracle database using to_sql. Below is the code:

engine = create_engine('oracle+cx_oracle://'+username+':'+password+'@'+host+':'+port+'/'+sid) df.to_sql(name = table.lower(),schema=schema,con =engine,if_exists = 'replace', index=False)

I am getting below error: UnicodeEncodeError: 'ascii' codec can't encode character '\u2013' in position 81: ordinal not in range(128)

can someone please help on this.

1 Answer 1

0

Probably your database is UTF8 and you did not export your NLS_LANG variable in your session:

So I would try to first export your NLS_LANG to the character set you have in your database:

#export NLS_LANG=American_America.AL32UTF8

Then I would try to change your connection in cx_Oracle.connect method:

connection = cx_Oracle.connect("user/password@connectString",
    encoding="UTF-8", nencoding="UTF-8")

As you are using to_sql , you might try this before you try your connection

import os
os.environ["NLS_LANG"] = 'YOUR_NLS_LANG_VARIABLE'
Sign up to request clarification or add additional context in comments.

3 Comments

only specifying NLS-LANG after importing libraries doesnt work. how can I insert whole df using .connect method ?
do you know the character set of your database ? what error do you get when you use the df.to_sql method
It's worth noting that cx_Oracle 8 was just released. (i) the default character set is now UTF-8 (ii) using NLS_LANG to set the character set no longer does anything.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.