如何将 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 03:53:37  来源:igfitidea点击:

How can I export contents of an oracle table to a file?

oracleexport

提问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

You can use Oracle's export tool: exp

可以使用Oracle的导出工具:exp

Edit:

编辑:

exp name/pwd@dbname file=filename.dmp tables=tablename rows=y indexes=n triggers=n grants=n

回答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时使用了这种方法