使用失败的Postgres / Psycopg2 copy_from没有记录错误

时间:2012-08-24 04:36:39

标签: python database postgresql psycopg2

以下代码不会引发错误。但它也没有在我的数据库中放置任何值。除主键上的索引外,已从表中取消所有约束。这两个字段都是字符串。有任何想法吗?最令人困惑的是没有记录错误。

conn = psycopg2.connect("dbname=<mydbname> user=postgres password=<mypassword>")
cur = conn.cursor()
output = StringIO.StringIO()
output.write('Citizen Caine\tMy_API_id\n')
cur.copy_from(output, 'movie', columns=('title','api_id'))
conn.commit()

1 个答案:

答案 0 :(得分:21)

我也在努力解决这个问题。一些隐藏的知识是你必须做的:

output.seek(0) #put the position of the buffer at the beginning

write之后,或者在您执行copy_to之后,如果您从数据库迁移到数据库。

很容易忘记StringIO个对象具有file object.

的所有相同方法和属性