python - 如何使用 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”,我在这里做错了什么?
解决方案
如果没有 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
推荐阅读
- vuejs2 - 在视图中显示组件属性时遇到问题
- php - 将两个查询合二为一?
- python - 如何在 Tkinter python 中为文本而不是为行添加背景
- c - 获得免费():执行代码时大小无效
- android - RoomDB - 你能加入来自不同数据库的表吗
- javascript - Three.js bloomPass 让整个场景变成 8 位的样子
- swift - Swift LayoutConstraint 在键盘框架更改时中断
- r - 如何从 R 中的 PTAK 包中重新排列 PARAFAC 模型的数组中的数据
- c++ - 使用三元运算符定义向量时出现主表达式错误
- mysql - json 路径中具有特殊字符的 MySQL