CX_ORACLE获取布尔返回值 [英] cx_Oracle Get Boolean Return Value

查看:20
本文介绍了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获取布尔返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆