Recently, in the project, we need to query a table with more than 1 million records and then make some data statistics. However, in this process, unicodeDecodeError occurs when only a few pieces of data are queried. Recently, in the project, we need to query a table with more than 1 million records and then make some data statistics. However, in this process, unicodeDecodeError occurs when only a few pieces of data are queried.
Here, we use the sqlalchemy library for query, and the corresponding operations are performed internally by Cx_Oracle. The Python version is 3.5.0, and the host system is Windows 2008 Server, perform the following operations:
from sqlalchemy import create_engineengine = create_engine('oracle://demo:123456@192.168.1.202/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 a connection to the database and then perform the corresponding query operation. Unfortunately, one UnicodeDecodeError error occurs when less than 10 records are queried.
We thought it was a database server encoding problem, so we added the encoding parameter to the create_engine function and changed it:
engine = create_engine('oracle://demo:123456@192.168.1.202/TEST',encoding="UTF-8")
The other method can be used to specify the encoding directly in the connection path, similar to the following:
engine = create_engine('oracle://demo:123456@192.168.1.202/TEST?charset=utf-8')
But the problem persists. I did not find a suitable solution after searching on the internet. I suddenly remembered that when I was using Mysql databases (I personally liked Postgresql more), there was a garbled code, we often perform the following operations:
set names gbk;
We set the client encoding in this way, rather than the server encoding, to solve the case of garbled code under the terminal (because Postgresql default database is the UTF-8 so there is a low possibility of garbled ). In addition, when installing the Oracle client in Linux, oneNLS_LANGFor more information about the environment variables, see the article Ubuntu14.04 installing Oracle Instant Client. of course, this article does not cover some details.
In general, we perform the following settings in cmd:
setenv NLS_LANG=SIMPLIFIED CHINESE_CHINA.ZHS16GBK
We specify the simplified Chinese language for Oracle messages, while the character set of the client is GBK.
In addition, we can execute the following statement to ensure that the above operations are correct:
SELECT * FROM v$nls_parameters;
Because the above database server is deployed on Windows, the result is naturally GBK. Therefore, if our client uses the UTF8 character set for decoding, a decoding error will naturally occur.
Note that non-ASCII encoding can be properly displayed only when the encoding of the database server and the client is consistent, in sqlalchemy, the query string is forcibly converted to Unicode by default. Therefore, the following process is similar to Python3:
>>> A = 'China'. encode ('gbk')> AB '\ xd6 \ xd0 \ xb9 \ xfa'
In sqlalchemy, the process is similar to the following:
>>> a.decode('utf-8')Traceback (most recent call last): File "
", line 1, in
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd6 in position 0: invalid continuation byte
As a result, the above problem occurs. In normal cases, you should specify its encoding as GBK:
>>> A. decode ('gbk') 'China'
And setNLS_LANGIt is equivalent to modifying the above encoding to GBK.
More Python-solve the UnicodeDecodeError problem in Cx_Oracle query. For more information, see PHP!