使用Jinjasql在Python中的模板查询的导入错误

发布于 2025-01-30 08:58:03 字数 1213 浏览 2 评论 0原文

因此,我试图根据呼叫创建具有某些参数的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

有深☉意 2025-02-06 08:58:04

这是您正在使用的库中的问题。已经报道了他们的问题跟踪器,请参见 https://github.com/sripathikrishnan/jinjasql jinjasql /eskoes/50

您可以做的是将jinja版本降级到 3.1.0 之前,这带来了删除markup逃脱。


在其中添加一条线一样简单

jinja2<3.1.0

pip install --requirements requirements.txt

如果您确实有一个 sumplion.txt 文件来安装依赖项,那么如果没有这样的文件,则 PIP,你可以做

pip install --upgrade 'jinja2<3.1.0'

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 and escape.


If you do have a requirements.txt file to install your dependencies, this is as simple as adding a line in it like

jinja2<3.1.0

And redo a

pip install --requirements requirements.txt

If you don't have such a file but are installing your packages with pip, you could do

pip install --upgrade 'jinja2<3.1.0'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文