如何解决 Python cx_oracle 中的 ORA-01704:字符串文字太长错误?

Di *_*Zou 3 python oracle cx-oracle

我正在尝试使用 Python cx_oracle 更新表中的条目。该列名为“template”,数据类型为 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()
Run Code Online (Sandbox Code Playgroud)

当我这样做时,我收到一条错误消息,指出字符串文字太长。模板变量包含大约 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)
Run Code Online (Sandbox Code Playgroud)

我收到“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)
Run Code Online (Sandbox Code Playgroud)

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

gec*_*cco 5

在 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()
Run Code Online (Sandbox Code Playgroud)