首页 > 解决方案 > 如何使用 SQLAlchemy 和 Python 更新布尔值?

问题描述

我正在尝试使用 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”,我在这里做错了什么?

标签: pythonsqlalchemy

解决方案


如果没有 table + schema 的示例或此函数的参数是什么样的(特别是hide_error),有点难以确定,但看起来可能存在问题hide_error == "True",因为它正在检查那hide_error是否是string "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

推荐阅读