使用SQLAlchemy和Python更新布尔值

2024-05-16 23:52:53 发布

您现在位置: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”,我在这里做错了什么


Tags: eventidtruestringtimeupdateerrorconnection
1条回答
网友
1楼 · 发布于 2024-05-16 23:52:53

如果没有table+模式的示例或者这个函数的参数是什么样子的(特别是hide_error),就有点难以确定,但是看起来hide_error == "True"行上可能有问题,因为它正在检查hide_error是否是字符串{},而不是布尔值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

相关问题 更多 >