Named Parameters in SQL Queries - cx_Oracle - ORA-01460: unimplemented or unreasonable conversion requested

偶尔善良 提交于 2019-12-11 09:43:14

问题


I have encountered a problem after implementing the named parameters in RAW SQL Queries as per Python DB-API.

Earlier, my code was as follows (and this works fine, both on my DEV Server and my Client's test server)

cursor.execute("SELECT DISTINCT(TAG_STATUS) FROM TAG_HIST WHERE TAG_NBR = '%s' " %(TAG_NBR))

I changed it to the following

cursor.execute("SELECT DISTINCT(TAG_STATUS) FROM TAG_HIST WHERE TAG_NBR = :TAG_NBR " ,{'TAG_NBR':TAG_NBR})

This changed version (with named parameters) works fine on my Development Server

  • Windows XP Oracle XE
  • SQL*Plus: Release 11.2.0.2.0
  • cx_Oracle-5.1.2-11g.win32-py2.7

However, when deployed on my Client's Test Server, it does not.... execution of all queries fail.

Characteristics of my client's server are as follows

  • Windows Server 2003
  • Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - 64bi
  • cx_Oracle-5.1.2-10g.win32-py2.7

The error that I get is as follows

Traceback (most recent call last):
  File "C:/Program Files/App_Logic/..\apps\views.py", line 400, in regularize_TAG
    T_cursor.execute("SELECT DISTINCT(TAG_STATUS) FROM TAG_HIST WHERE TAG_NBR = :TAG_NBR " ,{'TAG_NBR':TAG_NBR})
DatabaseError: ORA-01460: unimplemented or unreasonable conversion requested

Appreciate if someone could help me through this issue.

This issue presents itself only when the cx_Oracle code is run inside the Web App (Hosted on Apache).

If i run the same code with named parameters from within the python command line then the query runs just fine.


回答1:


Here is how this got solved.

I tried typecasting unicode to str and the results were positive.

This one worked for example

T_cursor.execute("SELECT DISTINCT(TAG_STATUS) FROM TAG_HIST WHERE TAG_NBR = :TAG_NBR", {'TAG_NBR': str(TAG_NBR)})

So in effect, unicode was getting mangled by getting encoded into the potentially non-unicode database character set.

To solve that, here is another option

import os
os.environ.update([('NLS_LANG', '.UTF8'),('ORA_NCHAR_LITERAL_REPLACE', 'TRUE'),])
import cx_Oracle

Above guarantees that we are really in UTF8 mode.

Second environment variable one is not an absolute necessity. And AFAIK there is no other way to set these variables (except before running app itself) due the fact that NLS_LANG is read by OCI libs from the environment.



来源:https://stackoverflow.com/questions/15797894/named-parameters-in-sql-queries-cx-oracle-ora-01460-unimplemented-or-unreas

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