首页 > 解决方案 > SQLAlchemy:如何在更新语句中使用变量

问题描述

我试图在更新语句中使用变量作为键,但似乎不允许使用变量。

有表“disamb”:

+----+-----------+--------------------+-----------+--------------------+  
| id | entity_a  | id_of_entity_a     | entity_b  | id_of_entity       |  
+----+-----------+--------------------+-----------+--------------------+  
| 0  | table     |                    | bar       | -                  |  
+----+-----------+--------------------+-----------+--------------------+  

和一个表“实体”:

+--------+----+-----------------------------------------------------------------+  
| entity | id |                           paraphrase                            |  
+--------+----+-----------------------------------------------------------------+  
| table  |  1 | An item of furniture with a flat top and one or more legs       |  
| table  |  2 | A data arrangement with rows and columns                        |  
| bar    |  3 | A retail business establishment that serves alcoholic beverages |  
| bar    |  4 | a long item                                                     |  
+--------+----+-----------------------------------------------------------------+  

我的程序循环遍历表“disamb”中的列“entity_a”和“entity_b”,并在表“entities”中搜索相应的单词,问我选择哪个,然后(应该)将其写入相应的列“id_of_entity_a”resp . “id_of_entity_b”。

因此,在循环的某一点,变量具有例如以下值:

column_name = "id_of_entity_a"  
id_of_entity = "1"  
row = "0"  

我尝试执行这段代码:

update_stmt = tbl.update().\
    values(column_name=id_of_entity).\
    where(tbl.columns.id == (row))
conn_mysql.execute(update_stmt)

它抛出错误:“sqlalchemy.exc.CompileError:未使用的列名:column_name”

我该如何解决这个问题?谢谢!

标签: pythonsqlalchemy

解决方案


推荐阅读