使用 psycopg2 和 python 上传数据
使用下一个命令,我尝试上传一个 csv 文件,其中列由制表符分隔,有时可以将空值分配给列。
conn = psycopg2.connect(host="localhost",
port="5432",
user="postgres",
password="somepwd",
database="mydb",
options="-c search_path=dbo")
...
cur = conn.cursor()
with open(opath, "r") as opath_file:
next(opath_file) # skip the header row
cur.copy_from(opath_file, table_name[3:], null='', columns=cols.split(','))
- cols 有一个字符串,列名以 ',' 分隔,
- 名为 table_name[3:] 的表属于 dbo 模式
此代码运行,没有报告错误,但没有上传数据。数据库的所有者是 postgres。
有什么想法吗?
With the next cmds I am trying to upload a csv file where columns are separated by tabs and sometimes null values can be assigned to a column.
conn = psycopg2.connect(host="localhost",
port="5432",
user="postgres",
password="somepwd",
database="mydb",
options="-c search_path=dbo")
...
cur = conn.cursor()
with open(opath, "r") as opath_file:
next(opath_file) # skip the header row
cur.copy_from(opath_file, table_name[3:], null='', columns=cols.split(','))
- cols has a string with the column names separated by ','
- the table with name table_name[3:] belongs to the dbo schema
This code runs, no error is reported but no data is uploaded. The owner of the db is postgres.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果问题是我需要
运行conn.commit()你会相信我吗?
在 cur.copy_from cmd 之后
Would you believe me if the problem was I needed to run
conn.commit()
after the cur.copy_from cmd?