Python psycopg2 copy_from() to load data throws error for null integer values: DataError: invalid input syntax for integer: “”

怎甘沉沦 提交于 2020-01-03 17:29:39

问题


I am trying to load data from a StringIO object of python into a Postgres database table using psycopg2's copy_from() method.

My copy_from fails on the first record itself specifically for a particular (nullable) integer column which has null value ('' without quotes). I have also tried using Python's None keyword instead of '' for NULL values. It throws me the following error: DataError: invalid input syntax for integer: "" CONTEXT: COPY , line 1, column : ""

The code looks something like this:

table_data = StringIO.StringIO()
# Populate the table_data variable with rows delimited by \n and columns delimited by \t
cursor = db_connection.cursor()
cursor.copy_from(table_data, <table_name>)

This column is a smallint column.


回答1:


By default, COPY FROM (and copy_from) encode a NULL value as \N. If you want to use the empty string to mean NULL, you need to say so explicitly:

cursor.copy_from(table_data, table_name, null="")


来源:https://stackoverflow.com/questions/26617895/python-psycopg2-copy-from-to-load-data-throws-error-for-null-integer-values-d

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!