如何在更新查询中的sqlalchemy中调用Postgresql sql函数 [英] How to call a postgresql sql function in sqlalchemy in an update query
问题描述
我是python中的sqlalachemy的新手,我有一个问题。
我需要移植此查询:
I am new to sqlalachemy in python and i have a question. I need to port this query:
UPDATE user_list SET last_visit=now() WHERE name='"+username+"'
我已经阅读了很多有关更新查询的信息,但是我认为可以设置last_visit为由postgresql的now()函数生成的值。我该怎么做呢?假设我将使用sqlalachemty的更新功能。我可以将其生成为sql字符串并执行(),但是该语句可能是sql注入的主题。
I have read a lot about the update query, but I see possibility to set the value of the last_visit to the value generated by the now() function of postgresql. How would I do that properly? Assume that I would use the update functions of sqlalachemty. I could generate this as sql string and execute() it, but then the statement could be subject of sql injection. How to do this right?
推荐答案
您可以使用 func.now()
(从 sqlalchemy
导入 func
)。
You can use func.now()
(import func
from sqlalchemy
).
参考: http://docs.sqlalchemy.org/en/latest/core/functions .html
这篇关于如何在更新查询中的sqlalchemy中调用Postgresql sql函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!