首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >python3.2 -使用cx-oracle更新clob

python3.2 -使用cx-oracle更新clob
EN

Stack Overflow用户
提问于 2012-11-15 05:02:15
回答 1查看 1.8K关注 0票数 0

我正在将一些代码从perl迁移到Python。我可以使用以下代码读取需要更新的CLOB:

代码语言:javascript
复制
cur.execute("""
SELECT
    , a.Category
    , a.Status
    …
    , c.name
    , a.orig_operator
FROM tables where stuff
""")

for result in cur:
    startTimes = result[18].read( # stringify from lob
    stopTimes = result[19].read()
    lobsterCode = result[17].read()

如何更新CLOB列?对于Perl,我选择for UPDATE,获取bin_locator,然后使用DBI中的ora_lob_write

我正在寻找展示Python等价物的示例。

EN

回答 1

Stack Overflow用户

发布于 2012-11-15 05:42:07

抱歉,我没有Python 3.2。这是在Python 2.7上。

在Oracle中:

代码语言:javascript
复制
scott@XE_11g> CREATE TABLE t (id NUMBER, c CLOB);

Table created.

scott@XE_11g> INSERT INTO t VALUES (1, 'Happy families are all alike; every unhappy family is unhappy in its own way.');

1 row created.

scott@XE_11g> INSERT INTO t VALUES (2, q'[You don't know about me without you have read a book by the name of The Adventures of Tom Sawyer; but that ain't no matter.]');

1 row created.

scott@XE_11g> COMMIT;

Commit complete.

scott@XE_11g>

现在,在Python中:

代码语言:javascript
复制
import cx_Oracle

connection = cx_Oracle.connect('scott/tiger@XE')
cursor = connection.cursor()
new_clob = cursor.var(cx_Oracle.CLOB)
new_clob.setvalue(0,'It was a bright cold day in April, and the clocks were striking thirteen. ')
key_id = cursor.var(cx_Oracle.NUMBER)
key_id.setvalue(0,2)

cursor.execute("""UPDATE t
                  SET    c = :p_clob
                  WHERE  id = :p_key"""
,              p_clob = new_clob
,              p_key = key_id
               )
connection.commit()

再回到Oracle:

代码语言:javascript
复制
scott@XE_11g> SELECT c FROM t WHERE id = 2;

C
----------------------------------------------------------------------------------------------------
It was a bright cold day in April, and the clocks were striking thirteen.

scott@XE_11g>

希望这能有所帮助。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13387268

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档