当前位置:   article > 正文

python2input空字符串出错,用于加载数据的Python psycopg2 copy_from()会为空整数值引发错误:DataError:整数的无效输入语法:“”...

python psycopg2 copy_from 字符集

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, )

This column is a smallint column.

解决方案

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="")

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/175852
推荐阅读
相关标签
  

闽ICP备14008679号