3

I am trying to insert Portuguese text into my table. But, it is giving 'ascii' codec can't encode character '\xea' error.

Here is what I am doing :

   os.environ["NLS_LANG"] = ".AL32UTF8"

   query = "INSERT INTO MESSAGE (MESSAGE,LANGUAGE) VALUES (:MESSAGE,:LANGUAGE)"
   data = {'MESSAGE': '..... assistência para ajuda responda AJUDA Sua', 'LANGUAGE': 'Portuguese'}
   cursor = conn.cursor()
   cursor.execute(query, data)
   .....

My table structure:

CREATE TABLE MESSAGE  (   
    language  VARCHAR2(12) NOT NULL, 
    message  NVARCHAR2(350) NOT NULL
);

I am not sure if I need to set anything to insert other characters into the database.

Sijan Bhandari
  • 2,941
  • 3
  • 23
  • 36

1 Answers1

7

If you are going to set the environment variable NLS_LANG you must do it before you create any connection; otherwise, it will have no effect. Better yet, however, is to use the following to create the connection as this does not depend on environment variables:

connection = cx_Oracle.connect("user/password@connectString",
        encoding="UTF-8", nencoding="UTF-8")
Anthony Tuininga
  • 6,388
  • 2
  • 14
  • 23