【发布时间】:2014-10-28 20:29:20
【问题描述】:
我正在尝试使用 psycopg2 的 copy_from() 方法将数据从 python 的 StringIO 对象加载到 Postgres 数据库表中。
我的 copy_from 在第一条记录本身上失败,特别是针对具有空值('' 不带引号)的特定(可为空)整数列。我也尝试过使用 Python 的 None 关键字而不是 '' 来表示 NULL 值。 它向我抛出以下错误: DataError:整数的输入语法无效:“” 上下文:复制,第 1 行,列:“”
代码如下所示:
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>)
此列是一个 smallint 列。
【问题讨论】:
标签: python postgresql psycopg2