从表中选择数据并插入到不同的数据库中
我正在使用 python 和 psycopg2 远程查询一些 psql 数据库,我试图找出从远程表中选择我需要的数据的最佳方法,并将其插入到单独的数据库(本地应用程序服务器)上的表中)。
我读过的大多数内容都指示我避免执行许多操作并寻求 COPY 操作,但我不确定如何在特定的 select 语句而不是整个表上实现这一点。我应该朝这个方向走还是我完全离开了?
I'm using python and psycopg2 to remotely query some psql databases, and I'm trying to figure out the best way to select the data I need from the remote table, and insert it into a table on a separate DB (local application server).
Most of the stuff I've read has directed me to avoid executemany and look toward COPY operations, but I'm unsure how to implement this on a specific select statement as opposed to the entire table. Should I be headed this way or am I completely off?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
COPY 不限于表,您也可以使用查询作为源,查看手册中的示例,它显示了如何使用 COPY 基于查询创建文本文件:
http: //www.postgresql.org/docs/current/static/sql-copy.html#AEN59055
(第三个例子)
COPY isn't limited to tables, you can use a query as the source as well, check out the examples in the manual, it shows how to use COPY to create a text file based on a query:
http://www.postgresql.org/docs/current/static/sql-copy.html#AEN59055
(3rd example)
看看 http://ryrobes.com/featured-articles/using-a-simple-python-script-for-end-to-end-data-transformation-and-etl-part-1/
当然,这是从 Oracle 中提取数据并插入到 SQL Server 中,但概念应该是相同的。
Take a look at http://ryrobes.com/featured-articles/using-a-simple-python-script-for-end-to-end-data-transformation-and-etl-part-1/
Granted, this is pulling from Oracle and inserting into SQL Server, but the concepts should be the same.