使用 Python 将 Oracle 数据库 table 导出为 XML 文件?
Export Oracle database table as XML file using Python?
我正在尝试将保存在 Oracle 12c 数据库中的 table 导出到组成 XML 文件中,这样 Oracle table 中的每一行都会产生 1 XML 文件。为此,我正在使用 Python 2.7 库 xml.etree.ElementTree
,但我在 documentation 中看不到任何允许我执行此操作的内容。在这一点上我基本上需要的是:
import cx_Oracle
from xml.etree import ElementTree as ET
SQL = ''.join([ 'SELECT * FROM ', table_name ])
connection = cx_Oracle.connect('username/password@database')
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 - 我无法在工作中下载第 3 方 Python 库. 在此先感谢您的帮助或建议。
您是否考虑过从数据库中返回 XML? Oracle DB 有很多 XML 支持。
这两个查询显示出不同的特征;检查 Oracle Manuals 其他人:
select xmlelement("Employees",
xmlelement("Name", employees.last_name),
xmlelement("Id", employees.employee_id)) as result
from employees
where employee_id > 200
和
select dbms_xmlgen.getxml('
select first_name
from employees
where department_id = 30') xml
from dual
[已解决] 为了将来参考,使用 Python 和 [=32= 将 Oracle 数据导出为 xml 格式需要两个单独的步骤].
1) 首先,出于某种原因,在 Python 的初始 SQL 语句中,我们必须使用 XML 类型 [=38] 的别名=] 我们正在尝试操作,并将 .getClobVal() 添加到 SQL 语句(第 3 行)中,如概述 因此,上面的代码变为:
1 import cx_Oracle
2
3 SQL = ''.join([ 'SELECT alias.COLUMN_NAME.getClobVal() FROM XML_TABLE ])
4 connection = cx_Oracle.connect('username/password@database')
5 cursor = connection.cursor()
2) 在我的问题中,我错误地使用了游标 - 因此需要第 12 行的附加代码:cx_Oracle.Cursor.fetchone()
。这实际上是 returns 一个元组,因此我们需要最后的 [0]
来切出元组中包含的单个信息。
此外,需要使用 str()
(第 13 行)将其转换为字符串。
完成此操作后,无需其他导入(例如 ElementTree)即可生成 xml 文件;这是在第 15-16 行完成的。
6 for i in range(num_rows):
7
8 file_name = i
9 file_path = ''.join([ 'C:\..., file_name, '.xml ])
10 file = open(file_path, 'w')
11 cursor.execute(SQL)
12 oracle_data = cx_Oracle.Cursor.fetchone(cursor_oracle_data)[0]
13 xml_data = str(oracle_data)
14
15 with open(file_path, 'w') as file:
16 file.write(xml_data)
17
18 file.close()
19
20 cursor.close()
21 connection.close()
我正在尝试将保存在 Oracle 12c 数据库中的 table 导出到组成 XML 文件中,这样 Oracle table 中的每一行都会产生 1 XML 文件。为此,我正在使用 Python 2.7 库 xml.etree.ElementTree
,但我在 documentation 中看不到任何允许我执行此操作的内容。在这一点上我基本上需要的是:
import cx_Oracle
from xml.etree import ElementTree as ET
SQL = ''.join([ 'SELECT * FROM ', table_name ])
connection = cx_Oracle.connect('username/password@database')
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 - 我无法在工作中下载第 3 方 Python 库. 在此先感谢您的帮助或建议。
您是否考虑过从数据库中返回 XML? Oracle DB 有很多 XML 支持。
这两个查询显示出不同的特征;检查 Oracle Manuals 其他人:
select xmlelement("Employees",
xmlelement("Name", employees.last_name),
xmlelement("Id", employees.employee_id)) as result
from employees
where employee_id > 200
和
select dbms_xmlgen.getxml('
select first_name
from employees
where department_id = 30') xml
from dual
[已解决] 为了将来参考,使用 Python 和 [=32= 将 Oracle 数据导出为 xml 格式需要两个单独的步骤].
1) 首先,出于某种原因,在 Python 的初始 SQL 语句中,我们必须使用 XML 类型 [=38] 的别名=] 我们正在尝试操作,并将 .getClobVal() 添加到 SQL 语句(第 3 行)中,如概述
1 import cx_Oracle
2
3 SQL = ''.join([ 'SELECT alias.COLUMN_NAME.getClobVal() FROM XML_TABLE ])
4 connection = cx_Oracle.connect('username/password@database')
5 cursor = connection.cursor()
2) 在我的问题中,我错误地使用了游标 - 因此需要第 12 行的附加代码:cx_Oracle.Cursor.fetchone()
。这实际上是 returns 一个元组,因此我们需要最后的 [0]
来切出元组中包含的单个信息。
此外,需要使用 str()
(第 13 行)将其转换为字符串。
完成此操作后,无需其他导入(例如 ElementTree)即可生成 xml 文件;这是在第 15-16 行完成的。
6 for i in range(num_rows):
7
8 file_name = i
9 file_path = ''.join([ 'C:\..., file_name, '.xml ])
10 file = open(file_path, 'w')
11 cursor.execute(SQL)
12 oracle_data = cx_Oracle.Cursor.fetchone(cursor_oracle_data)[0]
13 xml_data = str(oracle_data)
14
15 with open(file_path, 'w') as file:
16 file.write(xml_data)
17
18 file.close()
19
20 cursor.close()
21 connection.close()