SQLAlchemy Unicode Error - Querying Teradata database

。_饼干妹妹 提交于 2019-12-08 11:29:25

问题


I'm trying to use Python's SQLAlchemy library to query a Teradata database. I was able to create the engine okay using the following code.

from sqlalchemy import create_engine

td_engine = create_engine('teradata://' + 'usrname' + ':' + 'pswrd' + '@' + 'myOdbcDataSource' + ':22/?charset=UTF8')

But when I try to use the engine, I get the following error.

ValueError: character U+590048 is not in range [U+0000; U+10ffff]

This error occurs using all the functions that interact with the database that I tried. For example, I get this error when I try to execute the following.

sqlStr = 'select top 1000 * from myTable;' result = td_engine.execute(sqlStr)

As another example, I get the same error when I try to execute the following.

td_engine.table_names('mySchema')

The log right before the error indicates a connection to the database using the ODBC driver is being made so I wonder if this has something to do with the way I configured the ODBC driver. Below is my odbc.ini file located at /Library/ODBC/ on my Mac.

[ODBC Data Sources]
myodbca       = MySQL ODBC 5.3 ANSI Driver
myodbc        = MySQL ODBC 5.3 Unicode Driver
myOdbcDataSource        = Teradata Database ODBC Driver 16.20

[myOdbcDataSource]
Driver                = /Library/Application Support/teradata/client/16.20/lib/tdataodbc_sbu.dylib
DBCName               = myUrl
DefaultDatabase       = myDb
UserName              = usrname
Password              = pswrd
CharacterSet          = UTF8

Does anyone have any ideas on how to fix this Unicode error and get the SQL Alchemy Teradata engine working? Thank you in advance.


回答1:


Try to adjust encoding to UTF8:

from os import environ

environ["NLS_LANG"] = ".AL32UTF8"


来源:https://stackoverflow.com/questions/50024626/sqlalchemy-unicode-error-querying-teradata-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!