The following code does not elicit an error. But it doesn't put any values in my database either. Have taken all constraints off the table except index on Primary Key. The two fields are both strings. Any ideas? The most confusing thing is that no errors get logged.
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()
I struggled with this one too. Some hidden knowledge is that you have to do an:
output.seek(0) #put the position of the buffer at the beginning
after your write
, or if you're moving from database to database, after you do a copy_to
.
It's easy to forget that StringIO
objects have all the same methods and attributes of a file object.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With