我試圖將Oracle 12c數據庫中保存的表導出爲組成XML文件,以便Oracle表中的每一行生成1個XML文件。爲此,我正在使用Python 2.7庫xml.etree.ElementTree
,但我在documentation中看不到任何可以讓我這樣做的任何內容。基本上我需要在這一點上是:使用Python將Oracle數據庫表導出爲XML文件?
import cx_Oracle
from xml.etree import ElementTree as ET
SQL = ''.join([ 'SELECT * FROM ', table_name ])
connection = cx_Oracle.connect('username/[email protected]')
cursor = connection.cursor()
for i in range(num_rows):
... #code works fine up to here
file_name = i
file_path = ''.join([ 'C:\..., file_name, '.xml ])
file = open(file_path, 'w')
cursor.execute(SQL)
ET.ElementTree.write(file) #This line won't work
cursor.close()
file.close()
connection.close()
我知道它只會是1行代碼 - 我真的不知道該怎麼辦。
作爲一個增加的複雜性,我不幸只能使用Python 2.7本地庫,如etree - 我無法在工作中下載第三方Python庫。在此先感謝您的任何幫助或建議。
'write()'是'ElementTree'實例上的方法,但您可以在類上調用它。表示您的表格行的XML結構的根元素應該是一個參數。 'ET.ElementTree(root).write(file)'是否工作(假設'root'是那個根元素)? – mzjn