Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No errors logged with failed Postgres/Psycopg2 copy_from

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()
like image 249
Mike Girard Avatar asked Aug 24 '12 04:08

Mike Girard


1 Answers

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.

like image 174
notbad.jpeg Avatar answered Nov 05 '22 06:11

notbad.jpeg