SQLAlchemy:如何在更新语句中使用变量

时间:2019-06-20 11:28:30

标签: python sqlalchemy

我正在尝试在update语句中的键中使用变量,但似乎不允许使用变量。

有表格“ 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”分别。 “ 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”

如何解决此问题?谢谢!

0 个答案:

没有答案