This article and we share the main is to use the Python language to solve cx_oracle query unicodedecodeerror problem method steps, come together to see it, We hope to help you. recently, in the project , to the 1 Records of the table of more than one query , And then do some statistics , But in this process , we found that only a few data query came out unicodedecodeerror up. here , we use the sqlalchemy Library to query , its internal or cx_ Oracle to perform the corresponding operation , using the Python version of 3.5.0, The host system is Windows Server, and then you do something like the following : From SQLAlchemy import create_engine engine = Create_engine (' oracle://demo:[email protected]/test ') conn = Engine.connect () sql = ' Select T.type from TS t ' result = Conn.execute (SQL) for row in result: Print (row[' type ') here , we first create 1 connections to the database and then perform the corresponding query operations. Unfortunately , There are 1 of records that are not being queried. Unicodedecodeerror was wrong. I thought it was a database server coding problem , so I added encoding to the create_engine function. Parameters , change it to :engine = Create_engine (' Oracle://demo:[email protected]/test ', encoding= "UTF-8") The other 1 available ways to specify the encoding directly in the connection path are similar to the following : engine = Create_engine (' oracle://demo:[email protected]/test?charset=utf-8 ') But the problem is still unresolved. Search on the internet did not find a solution to adapt , suddenly remembered that before the use of Mysql database , when garbled. , We often do the following : Set names GBK; In this way, we set the client's code , not the server-side encoding , to solve the problem of garbled under the terminal. In addition , when installing Oracle 's client under Linux , 1 Nls_lang Environment Variables , Please refer to Ubuntu14.04 installation Oracle Instant Client This article, of course, has some details about this article without introduction. in General , we make the following settings in cmd : Setenv nls_lang=simplified Chinese_china. ZHS16GBK We specify that Oracle messages use the same language as Simplified Chinese , while the client's character set is GBK. In addition , we can execute the following statements to ensure that the above operation is correct : SELECT * from v$nls_parameters; because the above database servers are deployed on Windows , the results are naturally GBK, So if our clients use UTF8 character set for decoding , naturally, there will be decoding errors. We need to note that only if the database server side and the client's code is consistent , We can normally display non- ASCII Coding , and in SQLAlchemy The query's string is forced by default to convert it to Unicode . So The following process is similar to Python3 : >>> a= ' China ' encode (' GBK ') >>> a B ' \xd6\xd0\xb9\xfa ' in SQLAlchemy, because of the forced encoding conversion , Similar procedures are performed : >>> a.decode (' utf-8 ') Traceback (most recent): File "", Line 1, in <module> unicodedecodeerror: ' utf-8 ' codec can ' t decode byte 0xd6 in position 0:i Nvalid Continuation byte Therefore, the above problems have arisen. The normal situation should be to specify that it is encoded as GBK:>>> a.decode (' GBK ') ' China ' and setting Nls_lang is equivalent to modifying the above code to GBK.
Source: The Kite in the wind
Unicodedecodeerror problems when using Python to troubleshoot cx_oracle queries