Postgres Upsert中的TypeError

时间:2019-03-15 12:38:27

标签: postgresql jupyter upsert

我想打乱postgres(插入或更新)

如果表中不存在user_id,我想插入一行,其中user_id = user_id,first_date = date,last_date = date。但是如果用户已经存在,我只想用日期更新last_date。

我遇到错误,因此尝试使用以下方法简化插入操作:

pd.read_sql("INSERT INTO table (user_id, first_date, last_date) 
             VALUES ( "+str(user_id)+", '"+date+"','"+date+"')
             ON CONFLICT (user_id)
             DO NOTHING", conn)

但是我收到此错误“ TypeError:'NoneType'对象不可迭代”

我不明白是怎么回事...

我正在使用Jupyter和Postgres v11

1 个答案:

答案 0 :(得分:0)

read_sql期望查询返回数据。您的B&没有。

您可以使用执行功能the documentation

INSERT

,或者您可以在查询中添加from pandas.io import sql sql.execute('INSERT INTO table_name VALUES(?, ?, ?)', engine, params=[('id', 1, 12.2, True)]) 子句。 See the excellent postgresql documentation

相关问题