使用Jinjasql在Python中的模板查询的导入错误
因此,我试图根据呼叫创建具有某些参数的SQL模板会更改。我回顾了许多其他方法,但我发现使用Jinjasql的方法更合适,可以阅读,以满足我的模块的要求。
我尝试创建一个如下(缩短代码)以创建一个模板的查询列表。
from jinjasql import JinjaSql
j = JinjaSql(param_style='pyformat')
class QueryTemplate(object):
def highest_pp_team_score(self, tour_name, year):
params = {'tour_name': [], 'year': []}
params['tour_name'].append(tour_name)
params['year'].append(year)
template = """
SELECT
id.match_id,
id.inning,
id.team
FROM
innings_deliveries as id
WHERE
id.name LIKE {{ tour_name }}
AND YEAR(id.start_date) = {{ year }}
"""
query, bind_params = j.prepare_query(template, params)
return query % bind_params
...当我尝试运行时,我会收到以下错误。它在导入本身上。
ImportError: cannot import name 'Markup' from 'jinja2.utils'
在阅读了有关此问题的信息之后,我找不到对我的案件的任何解决方案,但是我确实找到了线程也有类似的问题。它需要我在导入中添加几行。由于最初的问题与烧瓶有关,因此我无法将其实施。
So, I'm trying to create SQL templates with certain parameters would change based on the call. I reviewed many other methods but I found the method using JinjaSql more suitable and readable for the requirement of my module.
I tried creating a class as below (code shortened) to create a templated list of queries.
from jinjasql import JinjaSql
j = JinjaSql(param_style='pyformat')
class QueryTemplate(object):
def highest_pp_team_score(self, tour_name, year):
params = {'tour_name': [], 'year': []}
params['tour_name'].append(tour_name)
params['year'].append(year)
template = """
SELECT
id.match_id,
id.inning,
id.team
FROM
innings_deliveries as id
WHERE
id.name LIKE {{ tour_name }}
AND YEAR(id.start_date) = {{ year }}
"""
query, bind_params = j.prepare_query(template, params)
return query % bind_params
... when I try and run, I get the below error. It's on the import itself.
ImportError: cannot import name 'Markup' from 'jinja2.utils'
After reading about the issue, I couldn't find any resolutions to my case but I did find a thread with similar issue. It needed me to add few lines to the import. Since, the original issue was related to flask, I couldn't implement it to my case.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是您正在使用的库中的问题。已经报道了他们的问题跟踪器,请参见 https://github.com/sripathikrishnan/jinjasql jinjasql /eskoes/50
您可以做的是将jinja版本降级到 3.1.0 之前,这带来了删除
markup
和逃脱。
在其中添加一条线一样简单
像
如果您确实有一个 sumplion.txt 文件来安装依赖项,那么如果没有这样的文件,则 PIP,你可以做
This is an issue in the library you are using. It has been reported on their issue tracker already, see https://github.com/sripathikrishnan/jinjasql/issues/50
What you could do is to downgrade your Jinja version to the one before 3.1.0 that brought those breaking changes of removing
Markup
andescape
.If you do have a requirements.txt file to install your dependencies, this is as simple as adding a line in it like
And redo a
If you don't have such a file but are installing your packages with pip, you could do