2012-11-14 84 views
0

我將一些代碼從perl遷移到python。我可以讀取CLOB我需要用下面的代碼更新:python3.2 - 使用cx-oracle更新clob

* cur.execute( 「」」選擇,a.Category ,a.Status ... ,c.name ,a.orig_operator FROM表 where stuff「」「) for result in cur: startTimes = result [18] .read()#stringify from lob stopTimes = result [19] .read() lobsterCode = result [17] .read )*

我似乎找不到關於如何更新一個clob列的例子?使用perl我選擇更新,獲取bin_locator,然後使用DBI中的ora_lob_write。我正在尋找示例顯示python equilivent

回答

1

道歉,我沒有Python 3.2。這是Python 2.7。

在Oracle:

[email protected]_11g> CREATE TABLE t (id NUMBER, c CLOB); 

Table created. 

[email protected]_11g> INSERT INTO t VALUES (1, 'Happy families are all alike; every unhappy family is unhappy in its own way.'); 

1 row created. 

[email protected]_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. 

[email protected]_11g> COMMIT; 

Commit complete. 

[email protected]_11g> 

現在,在Python:

import cx_Oracle 

connection = cx_Oracle.connect('scott/[email protected]') 
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() 

而且,早在甲骨文再次:

[email protected]_11g> SELECT c FROM t WHERE id = 2; 

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

[email protected]_11g> 

希望這有助於。

+0

是我的表中的列名或特殊的內部變量? – Kevin

+0

@Kevin:不是內部的,它是Oracle表中列的名稱(打算成爲主鍵)。我的''WHERE id =:p_key「'是爲你的例子中任何'」WHERE STUFF「'代表的簡寫。上面的例子中的 – Tebbe

+0

可以使用相同的光標選擇和更新嗎? – Kevin