psycopg2.DataError:整数的输入语法无效

时间:2017-08-22 09:52:03

标签: python postgresql pandas psycopg2

我正在尝试尽快将pandas数据帧插入到postgres表中。我尝试过executemany但它太慢了。插入24000行需要6个小时。大多数在线解决方案都表明psycopg2的copy_from是一种更快的方法。我使用以下代码将数据帧插入postgres表。

f = io.StringIO()
df.to_csv(f, index=False, header=False)
f.seek(0)

con = psycopg2.connect(**config_dev)
cur = con.cursor()
cur.copy_from(f, 'table', null="", sep=",")
con.commit()
cur.close()

但我收到此错误,即psycopg2.DataError: invalid input syntax for integer: "10% off on shoulder bags" CONTEXT: COPY table, line 1, column coupon_id: "10% off on shoulder bags"。这里下一列的文本试图插入到coupon_id中,这是一个整数。这是因为我在dataframe中的第一列被插入postgres表中的primary_key id,这会混乱列,从而导致数据错误。有人有解决方案吗?

0 个答案:

没有答案