【问题标题】:How can I Update Boolean Value Using SQLAlchemy and Python?如何使用 SQLAlchemy 和 Python 更新布尔值?
【发布时间】:2020-05-20 22:13:27
【问题描述】:

我正在尝试使用 Python 和 SQLAlchemy 更新数据库中的布尔值。这是我的代码:

def update_record_to_hide_or_show(e2e_id, hide_error, event_time, study_id):
    connection_string = _get_connection_string()
    db = create_engine(connection_string)
    roi_e2e_events = define_roi_e2e_events_table()
    with db.connect() as conn:
        if hide_error == "True":
            update = roi_e2e_events.update().values(hide_error=True).where(roi_e2e_events.c.e2e_id == e2e_id)\
            .where(roi_e2e_events.c.event_time == event_time)\
            .where(roi_e2e_events.c.study_id == study_id)
            print(update)
            result = conn.execute(update)
        else:
            update = roi_e2e_events.update().values(hide_error=False).where(roi_e2e_events.c.e2e_id == e2e_id) \
                .where(roi_e2e_events.c.event_time == event_time). \
                where(roi_e2e_events.c.study_id == study_id)
            result = conn.execute(update)
        return result

我能够毫无问题地输入条件的第一部分,当我尝试将查询提交到数据库时没有显示执行错误,我在单独的函数中创建了元数据并且更新查询看起来像这样:

UPDATE roi_e2e_events SET hide_error=:hide_error WHERE roi_e2e_events.e2e_id = :e2e_id_1 AND roi_e2e_events.event_time = :event_time_1 AND roi_e2e_events.study_id = :study_id_1

我没有看到布尔值在运行后更改为“True”,我在这里做错了什么?

【问题讨论】:

    标签: python sqlalchemy


    【解决方案1】:

    如果没有表 + 架构的示例或此函数的参数是什么样的(特别是 hide_error),这有点难以确定,但看起来hide_error == "True" 行可能存在问题,因为它正在检查 hide_error 是否是 字符串 "True",而不是布尔值 True

    如果它实际上是一个布尔值,我们实际上可以通过使用not 运算符来解决检查它是什么值的整个问题。像这样的:

    def update_record_to_hide_or_show(e2e_id, hide_error, event_time, study_id):
        connection_string = _get_connection_string()
        db = create_engine(connection_string)
        roi_e2e_events = define_roi_e2e_events_table()
        with db.connect() as conn:
            # no if statement here
            update_query = roi_e2e_events.update().values(
                hide_error=not hide_error # but notice the `not` here
            ).where(
                roi_e2e_events.c.e2e_id == e2e_id
            ).where(
                roi_e2e_events.c.event_time == event_time
            ).where(
                roi_e2e_events.c.study_id == study_id
            )
    
            result = conn.execute(update)
            return result
    

    此外,如果正在从数据库中检索 hide_error,您可以将其全部捆绑到单个 UPDATE 查询中,如下所示

    from sqlalchemy import not_ # this is used to invert the value of a boolean column in a query - on the database, rather than a value we have stored locally in a variable
    
    def update_record_to_hide_or_show(e2e_id, event_time, study_id): # notice that `hide_error` is gone
        connection_string = _get_connection_string()
        db = create_engine(connection_string)
        roi_e2e_events = define_roi_e2e_events_table()
        with db.connect() as conn:
            # still no if statement here
            update_query = roi_e2e_events.update().values(
                hide_error=not_(roi_e2e_events.c.hide_error) # here we use column, rather than its value, much like the `where` clauses
            ).where(
                roi_e2e_events.c.e2e_id == e2e_id
            ).where(
                roi_e2e_events.c.event_time == event_time
            ).where(
                roi_e2e_events.c.study_id == study_id
            )
    
            result = conn.execute(update)
            return result
    

    update_query 应该如下所示:

    UPDATE roi_e2e_events
    SET hide_error=NOT roi_e2e_events.hide_error
    WHERE roi_e2e_events.e2e_id = :e2e_id_1
    AND   roi_e2e_events.event_time = :event_time_1
    AND   roi_e2e_events.study_id = :study_id_1
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-30
      • 2018-10-17
      • 2012-08-16
      • 1970-01-01
      相关资源
      最近更新 更多