postgresql - Psycopg2 copy_from throws DataError: invalid input syntax for integer -


i have table integer columns. using psycopg2's copy_from

conn = psycopg2.connect(database=the_database,                             user="postgres",                             password=password,                             host="",                             port="")  print "putting data in table: opened database successfully" cur = conn.cursor() open(the_file, 'r') f:     cur.copy_from(file=f, table = the_table, sep=the_delimiter)     conn.commit() print "successfully copied data database!" conn.close() 

the error says expects 8th column integer , not string. but, python's write method can read strings file. so, how import file full of string representation of number postgres table columns expect integer when file can have character representation of integer (e.g. str(your_number)).

you either have write numbers in integer format file (which python's write method disallows) or psycopg2 should smart enough conversion part of copy_from procedure, apparently not. idea appreciated.

i ended using copy_expert command. note on windows, have set permission of file. post useful setting permission.

with open(the_file, 'r') f:                     sql_copy_statement = "copy {table} '"'{from_file}'"' delimiter '"'{deli}'"' {file_type} header;".format(table = the_table,                                                                                                                      from_file = the_file,                                                                                                                      deli = the_delimiter,                                                                                                                      file_type = the_file_type                                                                                                                                                                                                                                                              )         print sql_copy_statement         cur.copy_expert(sql_copy_statement, f)         conn.commit() 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -