奇怪的 Oracle 错误:“文本格式无效”

发布于 2024-09-14 10:41:58 字数 592 浏览 9 评论 0原文

我正在尝试使用这段代码从 DATA_TYPE=NUMBER(1,0) 的列中获取一些数据:

import cx_Oracle
conn = cx_Oracle.connect(usr, pwd, url)
cursor = conn.cursor()
cursor.execute("SELECT DELETED FROM SERVICEORDER WHERE ORDERID='TEST'")
print(cursor.fetchone()[0])

它因此抱怨:

Traceback (most recent call last):
  File "main.py", line 247, in <module>
    check = completed()
  File "main.py", line 57, in completed
    deleted = cursor.fetchone()[0]
cx_Oracle.DatabaseError: OCI-22061: invalid format text [T

用 DATA_TYPE=VARCHAR2 的列替换“DELETED”列不会引发这样的抱怨。

I'm trying to fetch some data from a column whose DATA_TYPE=NUMBER(1,0) with this piece of code:

import cx_Oracle
conn = cx_Oracle.connect(usr, pwd, url)
cursor = conn.cursor()
cursor.execute("SELECT DELETED FROM SERVICEORDER WHERE ORDERID='TEST'")
print(cursor.fetchone()[0])

which complains thus:

Traceback (most recent call last):
  File "main.py", line 247, in <module>
    check = completed()
  File "main.py", line 57, in completed
    deleted = cursor.fetchone()[0]
cx_Oracle.DatabaseError: OCI-22061: invalid format text [T

Replacing 'DELETED' column with one whose DATA_TYPE=VARCHAR2 does not throw such a complaint.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

她说她爱他 2024-09-21 10:41:59

我现在使用带有 Unicode 支持的 cx_Oracle 5.0.4 遇到这个问题。上述接受的解决方案对我不起作用。问题中的 DELETED 列是一个数字列,这就是导致此错误的原因。

根据邮件列表( http://comments.gmane.org /gmane.comp.python.db.cx-oracle/2390 )这可能是 Oracle 中的一个错误,仅在支持 Unicode 的 cx_Oracle 中显示。

从链接:
“当我在没有 Unicode 支持的情况下构建 cx_Oracle 时,一切都按预期工作。
当我构建带有 Unicode 支持的 cx_Oracle 时,尝试使用查询
返回一个数值(例如):

con = Connection( ... )
cur = con.cursor()
cur.execute( 'SELECT 1 FROM DUAL' )
rows = cur.fetchall()

导致此异常:

cx_Oracle.DatabaseError: OCI-22061: invalid format text [T

我为解决这个问题所做的工作是在 select 语句上执行:

cur.execute( 'SELECT to_char(1) FROM DUAL' )
rows = cur.fetchall()
for row in rows:
    val = int(row[0])

它非常丑陋,但它有效。

I am running in to this problem now using cx_Oracle 5.0.4 with Unicode support. The above accepted solution did not work for me. The DELETED column in the question is a Numeric column, which is what causes this bug.

According to the mailing list ( http://comments.gmane.org/gmane.comp.python.db.cx-oracle/2390 ) it may be a bug in Oracle that shows only in cx_Oracle with Unicode support.

from the link:
"When I build cx_Oracle without Unicode support, it all works as expected.
When I build cx_Oracle with Unicode support, attempting to use a query
that returns a numeric value (such as):

con = Connection( ... )
cur = con.cursor()
cur.execute( 'SELECT 1 FROM DUAL' )
rows = cur.fetchall()

results in this exception:

cx_Oracle.DatabaseError: OCI-22061: invalid format text [T

"

What I did to work around it, is on the select statement, do:

cur.execute( 'SELECT to_char(1) FROM DUAL' )
rows = cur.fetchall()
for row in rows:
    val = int(row[0])

It's pretty ugly, but it works.

空城仅有旧梦在 2024-09-21 10:41:59

当我升级到 cx_Oracle 5.1 后,这些类型的错误就消失了。如果 RPM 没有安装(就像我在 Red Hat 5.5 上发生的那样),那么您通常可以 rpm2cpio 文件,获取 cx_Oracle.so 并将其放入您的 python site-packages 目录中。

These types of errors went away when I upgraded to cx_Oracle 5.1. If the RPM doesn't install (like it happened for me on Red Hat 5.5) then you can usually rpm2cpio the file, take the cx_Oracle.so and put it into your python site-packages directory.

陪我终i 2024-09-21 10:41:59

解决方法是将 time.sleep(1) 放在 cursor.fetchone() 之前:

...
cursor.execute("SELECT DELETED FROM SERVICEORDER WHERE ORDERID='TEST'")
time.sleep(1)
print(cursor.fetchone()[0])

A work-around is putting time.sleep(1) before cursor.fetchone():

...
cursor.execute("SELECT DELETED FROM SERVICEORDER WHERE ORDERID='TEST'")
time.sleep(1)
print(cursor.fetchone()[0])
扮仙女 2024-09-21 10:41:59

我有同样的错误。

承诺帮助我:

conn = cx_Oracle.connect(...)
...
cursor.execute()
conn.commit()

I had the same error.

Commit helped me:

conn = cx_Oracle.connect(...)
...
cursor.execute()
conn.commit()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文