如何将 oracle 表的内容导出到文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9021810/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I export contents of an oracle table to a file?
提问by chris
Getting ready to clean up some old tables which are no longer in use, but I would like to be able to archive the contents before removing them from the database.
准备清理一些不再使用的旧表,但我希望能够在从数据库中删除它们之前将内容存档。
Is it possible to export the contents of a table to a file? Ideally, one file per table.
是否可以将表的内容导出到文件?理想情况下,每个表一个文件。
回答by vc 74
回答by olekb
You can easily do it using Python and cx_Oracle module. Python script will extract data to disk in CSV format.
您可以使用 Python 和 cx_Oracle 模块轻松完成。Python 脚本将以 CSV 格式将数据提取到磁盘。
Here's how you connect to Oracle using Python/cx_Oracle:
以下是使用 Python/cx_Oracle 连接到 Oracle 的方法:
constr='scott/tiger@localhost:1521/ORCL12'
con = cx_Oracle.connect(constr)
cur = con.cursor()
After data fetch you can loop through Python list and save data in CSV format.
获取数据后,您可以遍历 Python 列表并以 CSV 格式保存数据。
for i, chunk in enumerate(chunks(cur)):
f_out.write('\n'.join([column_delimiter.join(row[0]) for row in chunk]))
f_out.write('\n')
I used this approach when I wrote TableHunter-For-Oracle
我在写TableHunter-For-Oracle时使用了这种方法