简体   繁体   English

如何根据条件从 Python 更新 Postgres 表中的列值

[英]How to update column value in Postgres table from Python based on condition

I have postgres table (Alpha) with following values我有具有以下值的 postgres 表(Alpha)

Alpha Α

ID  Exp_Date     Active_Status
1   7-12-2021    active
2   8-12-2021    active
3   15-12-2021   active
4   15-12-2021   active
5   11-12-2021   expired
6   1-12-2021    expired
7   6-12-2021    expired

My Connection String我的连接字符串

postgres_str = f'postgresql://{username}:{password}@{host}:{port}/{dbname}'
# Create the connection and cursor
rds_conn = psycopg2.connect(postgres_str)
rds_cur = rds_conn.cursor()

I want to update the Active_Status to expired based on two conditions我想根据两个条件将Active_Status更新为已过期

  1. if Exp_date is less than system date ( todays date)如果Exp_date小于system date ( todays date)
  2. if Active_Status ='active'如果Active_Status ='active'

Expected Output预计 Output

ID  Exp_Date     Active_Status
1   7-12-2021    expired
2   8-12-2021    expired
3   15-12-2021   active
4   15-12-2021   active
5   11-12-2021   expired
6   1-12-2021    expired
7   6-12-2021    expired

How could the update query written from python for postgres从 python 为 postgres 编写的更新查询如何

  1. I would learn to stay away from f strings when working with databases.在使用数据库时,我会学会远离 f 字符串。 I would suggest make_dsn我会建议make_dsn

  2. It is just a SQL query, Python does not really enter in to it.它只是一个 SQL 查询,Python 并没有真正进入它。 Test in psql:在 psql 中测试:

BEGIN;  
update 
   "Alpha" 
set 
   "Active_Status" = 'expired' 
where 
   "Exp_date" < current_date 
and 
   "Active_Status" = 'active';
--Then either COMMIT; or ROLLBACK; depending on whether it worked or not.

Then you can transfer the query to your psycopg2 code.然后您可以将查询转移到您的 psycopg2 代码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 更新 python 中的 postgres 表列值? - update a postgres table column value in python? Python - 如何根据特定条件迭代行并更新列值? - Python - How to iterate rows and update the column value based on a certain condition? 根据python中的条件使用第1行或第2行的值更新数据框列 - Update dataframe column based with value from either row 1 or row 2 based on condition in python 如何根据使用 Pyspark 的条件从另一个表更新 Spark DataFrame 表的列值 - How to update Spark DataFrame Column Values of a table from another table based on a condition using Pyspark for 循环,用于根据 Python 中的条件将每一行的值与另一个表中的特定列相乘 - for Loop for multiplying a value of each row with a specific column from another table based on condition in Python 如何使用 python 和 boto3 根据条件更新 dynamodb 表中的值? - how to update a value in dynamodb table based on condition using python and boto3? 如何根据 python 中另一列的条件查找两个日期之间特定列的最大值 - How do I Find max value of a particular column between 2 dates based on a condition from another column in python 根据条件python将dict值从一个更新为另一个 - update dict value from one to another based on condition python 根据python中的条件更新多列值 - Update muliple column values based on condition in python 根据 python 中列的条件更新行值 - Update row values based on condition on column in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM