使用psycopg2库复制命令 [英] copy command with psycopg2 library
本文介绍了使用psycopg2库复制命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以通过psycopg2运行以下命令?如果是这样,我该怎么办?
Is it possible to run the command below through psycopg2? If so, how can I do it?
COPY table_name(col1,col2) FROM 'path/to/file.csv' WITH HEADER DELIMITER ',' CSV;
推荐答案
是!
您可以使用 copy_from
方法:
You can use the copy_from
method:
import psycopg2
dbname=...
user=...
password=...
host=...
port=...
con = psycopg2.connect(database=dbname,user=user,password=password,host=host,port=port)
cur = con.cursor()
f = open('path/to/file.csv')
cur.copy_from(f, 'test', columns=('col1', 'col2'), sep=",")
con.commit()
con.close()
这篇关于使用psycopg2库复制命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文