Mik*_*ard 16 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()
Run Code Online (Sandbox Code Playgroud)
not*_*peg 24
我也在努力解决这个问题.一些隐藏的知识是你必须做的:
output.seek(0) #put the position of the buffer at the beginning
Run Code Online (Sandbox Code Playgroud)
在您之后write,或者如果您从数据库迁移到数据库之后,请执行此操作copy_to.
很容易忘记StringIO对象具有文件对象的所有相同方法和属性.