如何解决Python cx_oracle中ORA-01704:字符串文字太长的错误?
我正在尝试更新usinig Python cx_oracle表中的条目。该列被命名为“模板”,并且其数据类型为CLOB。如何解决Python cx_oracle中ORA-01704:字符串文字太长的错误?
这是我的代码:
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:非法的变量名/编号错误”
EDIT2 :
所以这是我现在的代码:
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:无效字符错误。
回答:
在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() 
回答:
更改您的表格定义。一个varchar2字段最多可以存储32767个字节;所以,如果你使用的是8位编码,那么在使用LOB之前,你还有一点空间可以使用。
回答:
使用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() 
以上是 如何解决Python cx_oracle中ORA-01704:字符串文字太长的错误? 的全部内容, 来源链接: utcz.com/qa/263896.html

