根据同一行中的多列值过滤并更新每行的特定列python sqlalchemy

时间:2019-12-13 09:57:34

标签: python-3.x orm sqlalchemy

这里是SQL table

|---------------------|------------------|---------------------|------------------|
|          ID         |     StartDate    |        EndDate      |       Status     |
|---------------------|------------------|---------------------|------------------|
|          0          | 2019-12-19T10:00 |   2019-12-28T10:00  |       Active     |
|---------------------|------------------|---------------------|------------------|
|          1          | 2019-11-19T10:00 |   2019-11-28T10:00  |       Active     |
|---------------------|------------------|---------------------|------------------|
|          2          | 2019-12-13T10:00 |   2019-12-17T10:00  |       Active     |
|---------------------|------------------|---------------------|------------------|
|          3          | 2019-10-19T10:00 |   2019-10-28T10:00  |       Active     |
|---------------------|------------------|---------------------|------------------|
|          4          | 2019-12-24T10:00 |   2019-12-28T10:00  |       Active     |
|---------------------|------------------|---------------------|------------------|

我要基于同一行的两个列值StatusStartDate来更新每行的列EndDate

条件将是 if StartDate < current_date and EndDate < current_date

然后将特定行的特定列Status的值更新为Inactive

if current_date is 2019-12-13T10:00 这应该是该操作的结果输出为

|---------------------|------------------|---------------------|------------------|
|          ID         |     StartDate    |        EndDate      |       Status     |
|---------------------|------------------|---------------------|------------------|
|          0          | 2019-12-19T10:00 |   2019-12-28T10:00  |       Active     |
|---------------------|------------------|---------------------|------------------|
|          1          | 2019-11-19T10:00 |   2019-11-28T10:00  |     Inactive     |
|---------------------|------------------|---------------------|------------------|
|          2          | 2019-12-13T10:00 |   2019-12-17T10:00  |       Active     |
|---------------------|------------------|---------------------|------------------|
|          3          | 2019-10-19T10:00 |   2019-10-28T10:00  |     Inactive     |
|---------------------|------------------|---------------------|------------------|
|          4          | 2019-12-24T10:00 |   2019-12-28T10:00  |       Active     |
|---------------------|------------------|---------------------|------------------|

我尝试过 DBSession.query(User).filter(and_(User.c.Status=="Active",User.c.StartDate < current_date, User.c.EndDate < current_date)).update({"Status":"Inactive"})

即使是我尝试的

from sqlalchemy import and_, func, update

DBSession.query(User).filter(and_(User.c.Status=="Active",func.date(User.c.StartDate) < current_date, func.date(User.c.EndDate) < current_date)).update({"Status":"Inactive"}) 来源:SQLAlchemy: how to filter date field?

但是我得到这个错误

  

回溯(最近通话最近):文件   “ C:\ Users \ Pl \ Envs \ r \ lib \ site-packages \ flask \ app.py”,第2463行,在   致电       返回self.wsgi_app(环境,start_response)文件“ C:\ Users \ PL \ Envs \ r \ lib \ site-packages \ flask \ app.py”,行2449,在   wsgi_app       响应= self.handle_exception(e)文件“ C:\ Users \ Pl \ Envs \ r \ lib \ site-packages \ flask \ app.py”,行1866,在   handle_exception       重新提升(exc_type,exc_value,tb)文件“ C:\ Users \ Pl \ Envs \ r \ lib \ site-packages \ flask_compat.py”,第39行,在   加薪       提高价值文件“ C:\ Users \ Pl \ Envs \ r \ lib \ site-packages \ flask \ app.py”,第2446行,在   wsgi_app       响应= self.full_dispatch_request()文件“ C:\ Users \ Pl \ Envs \ r \ lib \ site-packages \ flask \ app.py”,行1951,在   full_dispatch_request       rv = self.handle_user_exception(e)文件“ C:\ Users \ Pl \ Envs \ r \ lib \ site-packages \ flask \ app.py”,行1820,在   handle_user_exception       重新提升(exc_type,exc_value,tb)文件“ C:\ Users \ Pl \ Envs \ r \ lib \ site-packages \ flask_compat.py”,第39行,在   加薪       提高价值文件“ C:\ Users \ Pl \ Envs \ r \ lib \ site-packages \ flask \ app.py”,行1949,在   full_dispatch_request       rv = self.dispatch_request()文件“ C:\ Users \ Pl \ Envs \ r \ lib \ site-packages \ flask \ app.py”,行1935,在   dispatch_request       在活动中返回self.view_functionsrule.endpoint文件“ D:\ cs \ serverv0.6.py”,第145行       DBSession.query(User).filter(and_(User.c.StartDate

出了什么问题?

1 个答案:

答案 0 :(得分:0)

我是这样找到的

active_users = userTableSession.query(userTable).filter(userTable.c.Status == 'Active').all()
active_users_df = pd.DataFrame(active_users)

# targeting the end date
active_users_df['EndDates'] = pd.to_datetime(active_users_df['EndDate'], format = '%Y-%m-%dT%H:%M')

# fetch the current date
current_date_time = datetime.now().strftime('%Y-%m-%dT%H:%M')

# pick out the rows end date have not expired
expired_user = active_users_df.loc[(active_users_df['EndDates'] < current_date_time)]

# fetch the user ID in a list
inactive_user_list = list(expired_user['ID'])
print(inactive_user_list)

# update the table
u = userTable.update().values(Status="Inactive").where(userTable.c.ID.in_(inactive_user_list))
userTableSession.execute(u)
userTableSession.commit()

请发布比这更懒的答案?