ckp ckp - 2 months ago 6
Python Question

Updating postgres column using python dataframe

I am generating dataframe (

df
) for different dates and it will have the following variables

date value rowId
2016-05-14 2.5 1
2016-05-14 3.0 2
2016-05-14 3.4 5


I have to update a column (
value
) in postgres table (
Table1
). This table already contains a column (
value
) of type decimal in (
Table1
) along with unique identifier (
rowId
)

for d in range(0,len(df)):
QUERY=""" UPDATE "Table1" SET "value"='%s' WHERE "Table1"."rowId"='%s'
""" % (df['value'][d], df['rowId'][d])
cur.execute(QUERY)


There is no error. However, the above code is not updating the column in Postgres table. Is there any error in the code?

Answer

Well, closing transaction with commit should help. I'm not sure if you are using psycopg2, or any other library which has a commit() function. If not, then a simple cur.execute('COMMIT') should be enough. This should be run right after the for loop.