ckp ckp - 1 year ago 109
Python Question

Updating postgres column using python dataframe

I am generating dataframe (

) 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 (
) in postgres table (
). This table already contains a column (
) of type decimal in (
) along with unique identifier (

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

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

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download