I have Postgresql table test_data as df1
dataframe like below-
|email_id| status |role
|abc@gmail.com | 1 |1
|abc@gmail.com | 1 |2
|def@gmail.com | 0 |2
|ghi@gmail.com | 1 |2
|mno@gmail.com | 3 |1
|pqr@gmail.com | 2 |1
|MNP@gmail.com | 1 |2
Another dataframe df2
as the updated one -
|email_id| status |role
|abc@gmail.com | 1 |1
|abc@gmail.com | 0 |2
|def@gmail.com | 2 |2
|ghi@gmail.com | 1 |2
|mno@gmail.com | 1 |1
|pqr@gmail.com | 0 |1
|MNP@gmail.com | 1 |2
I want to update POSTGRES table test_data fieldname status
in df1
from df2 based on email_id and role.
Used below approach to update the test_data table -
# importing psycopg2 module
import psycopg2
# establishing the connection
conn = psycopg2.connect(
database="postgres",
user='postgres',
password='password',
host='localhost',
port= '5432'
)
conn.autocommit=True
for i in df2.index:
status = df2['status'][i].item()
email_id = df2['email_id'][i]
role = df2['role'][i].item()
sql1 = "update test.test_data set status = %s where role=%s and email_id = %s"
cur.execute(sql1,[status,role,email_id])
cur.close()
conn.close()
Dtypes of DF2 -
email_id object
role int64
status int64
The data in Postgres table is not updating, its running with no errors. Please help .