CX_ORACLE获取布尔返回值
本教程将介绍CX_ORACLE获取布尔返回值的处理方法,这篇教程是从别的地方看到的,然后加了一些国外程序员的疑问与解答,希望能对你有所帮助,好了,下面开始学习吧。
问题描述
我整天都在努力工作,试图使用CX_ORACLE从PL/SQL函数获取布尔值。我见过一些帖子讨论使用其他数据类型(如char或整数)来存储返回值,但当我尝试使用这种解决方案时,我得到了一个不正确的数据类型错误。首先,让我展示一下代码。
def lives_on_campus(self):
cursor = conn.cursor()
ret = cursor.callfunc('students_api.lives_on_campus', bool, [self.pidm])
return ret
如果我使用11.2.0.4数据库客户端,则会出现以下错误。
File "student-extracts.py", line 134, in <module>
if student.lives_on_campus():
File "student-extracts.py", line 58, in lives_on_campus
ret = cursor.callfunc('students_api.lives_on_campus', bool, [self.pidm])
cx_Oracle.DatabaseError: DPI-1050: Oracle Client library is at version 11.2 but version 12.1 or higher is needed
如果我使用12.1.0.2或更高版本的数据库客户端,则会出现此错误。
Traceback (most recent call last):
File "student-extracts.py", line 134, in <module>
if student.lives_on_campus():
File "student-extracts.py", line 58, in lives_on_campus
ret = cursor.callfunc('students_api.lives_on_campus', bool, [self.pidm])
cx_Oracle.DatabaseError: ORA-03115: unsupported network datatype or representation
基本上,无论我使用哪个版本的SQL客户端,它都会出错。现在,我知道如果数据库版本是12c R2,上面的代码就可以工作了。遗憾的是,我们的测试环境中只有该版本,并且Prod只使用11g数据库。有什么什么我可以让这个功能在11G数据库上工作的方法?必须有解决办法。
~Bob
推荐答案
尝试包装匿名块:
with connection.cursor() as cursor:
outVal = cursor.var(int)
sql="""
begin
:outVal := sys.diutil.bool_to_int(students_api.lives_on_campus(:pidm));
end;
"""
cursor.execute(sql, outVal=outVal, pidm='123456')
print(outVal.getvalue())
好了关于CX_ORACLE获取布尔返回值的教程就到这里就结束了,希望趣模板源码网找到的这篇技术文章能帮助到大家,更多技术教程可以在站内搜索。