![](/img/trans.png)
[英]How can I query a database with a value to get another value with sqlalchemy?
[英]How can I get/set an SqlAlchemy query value with the Column object
為了使一些代碼更通用,我嘗試使用列 object 在數據庫行中檢索/設置值。
例如,我將進行以下設置:
col = MyTable.MyColumn
result = session.query(...).one()
現在我想獲取/設置值,語義如下:
cur_value = result[col]
result[col] = new_value
使用列 object 在結果中獲取/設置值的正確語法是什么?
col
不會是 static 值,而是動態取自 map。
你可以這樣做:
cur_value = getattr(result, col.key)
setattr(result, col.key, new_value)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.