我有一个数据库表,有将近一百万条记录。
我添加了一个新列,名为concentration
。
然后我有一个函数可以计算每个记录的“浓度”。在
现在,我想成批更新这些记录,所以我一直在研究以下问题/答案:https://stackoverflow.com/a/33258295/596841,https://stackoverflow.com/a/23324727/596841和{a3},但我不确定如何使用unnest
来完成这项工作。。。在
这是我的Python 3函数,用于执行更新:
def BatchUpdateWithConcentration(tablename, concentrations):
connection = psycopg2.connect(dbname=database_name, host=host, port=port, user=username, password=password);
cursor = connection.cursor();
sql = """
update #tablename# as t
set
t.concentration = s.con
FROM unnest(%s) s(con, id)
WHERE t.id = s.id;
"""
cursor.execute(sql.replace('#tablename#',tablename.lower()), (concentrations,))
connection.commit()
cursor.close()
connection.close()
concentrations
是元组的数组:
[(3.718244705238561e-16108264),(…)]
第一个值是双精度值,第二个值是整数,分别表示浓度和rowid。在
我得到的错误是:
psycopg2.ProgrammingError: a column definition list is required for functions returning "record" LINE 5: FROM unnest(ARRAY[(3.718244705238561e-16, 108264), (... ^
由于Psycopg将Python元组改编为Postgresql匿名记录,因此有必要指定数据类型:
相关问题 更多 >
编程相关推荐