【问题标题】:How do I solve the ORA-01704: string literal too long error in Python cx_oracle?如何解决 Python cx_oracle 中的 ORA-01704: string literal too long 错误?
【发布时间】:2012-01-27 01:29:30
【问题描述】:

我正在尝试使用 Python cx_oracle 更新表中的条目。该列名为“模板”,其数据类型为 CLOB。

这是我的代码:

dsn = cx_Oracle.makedsn(hostname, port, sid)
orcl = cx_Oracle.connect(username + '/' + password + '@' + dsn)
curs = orcl.cursor()
sql = "update mytable set template='" + template + "' where id='6';"
curs.execute(sql)
orcl.close()

当我这样做时,我收到一个错误,说字符串文字太长。模板变量包含大约 26000 个字符。我该如何解决?

编辑:

我发现了这个:http://osdir.com/ml/python.db.cx-oracle/2005-04/msg00003.html
所以我尝试了这个:

curs.setinputsizes(value = cx_Oracle.CLOB)
sql = "update mytable set template='values(:value)' where id='6';"
curs.execute(sql, value = template)

我收到“ORA-01036:非法变量名/编号错误”

编辑2:

这是我现在的代码:

    curs.setinputsizes(template = cx_Oracle.CLOB)
    sql = "update mytable set template= :template where id='6';"
    print sql, template
    curs.execute(sql, template=template)

我现在收到 ORA-00911:无效字符错误。

【问题讨论】:

  • 你错误使用了参数:将'values(:value)'改为:value另见我的回复

标签: python oracle cx-oracle


【解决方案1】:

更改您的表定义。一个varchar2字段最多可以存储32767个字节;因此,如果您使用的是 8 位编码,那么在使用 LOB 之前,您还有一些空间可以发挥作用。

【讨论】:

  • 该列的数据类型是 CLOB
  • @DiZou 这个信息很有趣:你应该把它添加到你的问题中。
【解决方案2】:

在 sql 语句中插入值是一种非常糟糕的做法。您应该改用参数:

dsn = cx_Oracle.makedsn(hostname, port, sid)
orcl = cx_Oracle.connect(username + '/' + password + '@' + dsn)
curs = orcl.cursor()
curs.setinputsizes(template = cx_Oracle.CLOB)
sql = "update mytable set template= :template where id='6'"
curs.execute(sql, template=template)
orcl.close()

【讨论】:

  • 我现在收到 ORA-00911: invalid character 错误。 sql语句在sqlplus和sql developer中有效。
  • 我不确定,但您能否尝试删除末尾的分号(请参阅我的更新)
【解决方案3】:

使用 IronPython

import sys
sys.path.append(r"...\Oracle\odp.net.11g.64bit")
import clr
clr.AddReference("Oracle.DataAccess")
from Oracle.DataAccess.Client import OracleConnection, OracleCommand,   OracleDataAdapter

connection = OracleConnection('userid=user;password=hello;datasource=database_1')
connection.Open()

command = OracleCommand()
command.Connection = connection
command.CommandText = "SQL goes here"
command.ExecuteNonQuery()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-31
    • 1970-01-01
    • 2019-12-28
    • 2016-01-25
    • 2021-09-27
    • 1970-01-01
    相关资源
    最近更新 更多