使用DATE_FORMAT在内联SQL查询的Python Flas问题

2024-09-24 00:33:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我的查询如下所示

result = app._engine.execute ("select bookingid, DATE_FORMAT(bookingdate, '%Y-%m-%d'), DATE_FORMAT(pickupdate, '%Y-%m-%d'), consigneeid, fromcity from booking where consignerid = '{0}'".format(loginid))

当我在MySQL Workbench中运行并返回格式为yyyy-mm-dd的日期时,这个查询有效,但是当我在python flask中运行该查询时,会出现以下错误

^{pr2}$

它正在寻找更多的参数,尽管查询中的%用于格式化而不是传递参数。我确实试着寻找原因,但找不到一个好的原因,我做错了什么?在


Tags: fromappformatexecute参数date原因result
1条回答
网友
1楼 · 发布于 2024-09-24 00:33:30

使用MySQL Connector for Python时,应该转义literal%,这是因为Python使用百分号(%)进行字符串格式设置。在

试试这个:

result = app._engine.execute ("select bookingid, DATE_FORMAT(bookingdate, '%%Y-%%m-%%d'), DATE_FORMAT(pickupdate, '%%Y-%%m-%%d'), consigneeid, fromcity from booking where consignerid = '{0}'".format(loginid))

相关问题 更多 >