私はほぼ100万レコードのデータベーステーブルを持っています。 concentration
という新しい列を追加しました。 私は各レコードの '濃度'を計算する関数を持っています。unnestを使用して複数のpostgresqlレコードを更新する
さて、私はバッチ内のレコードを更新したいので、私は、次の質問/回答を見てきた:https://stackoverflow.com/a/33258295/596841、https://stackoverflow.com/a/23324727/596841とhttps://stackoverflow.com/a/39626473/596841が、私は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-16、108264)、(... )]
最初の値は倍精度で、2番目の値はそれぞれ濃度とROWIDを表す整数です。
私は取得していますエラーは次のとおりです。
psycopg2.ProgrammingError: a column definition list is required for functions returning "record" LINE 5: FROM unnest(ARRAY[(3.718244705238561e-16, 108264), (... ^
Aaaargh!私は、データ型を追加しようとしたが、私は誤って変数の前に置く... – pookie