简体   繁体   English

python 用于插入语句的神社模板

[英]python jinja template for insert statement

Have an sql query:有一个 sql 查询:

insert into my_schema.table1(
    f1,
    f2,
    f3,
    f4
) values (%s, %s, %s, %s)

And I need to create a jinja template for it.我需要为它创建一个神社模板。 My suggestion is to create the following template:我的建议是创建以下模板:

insert into {{ mysql_schema }}.{{ table_name }} (
    {%- for feature in features %}
        {{ feature }} {{ "," if not loop.last }}
    {%- endfor %}
) values ({%- for feature in features %}
                {{ %s }} {{ "," if not loop.last }}
          {%- endfor %}
)

But in this case I get an error:但在这种情况下,我得到一个错误:

jinja2.exceptions.TemplateSyntaxError: unexpected '%'

How can I fix this error?我该如何解决这个错误?

You should output %s literally, rather than letting Jinja2 evaluate it by enclosing it in double curly braces, resulting in the said syntax error.你应该 output %s从字面上看,而不是让 Jinja2 通过将它括在双花括号中来评估它,从而导致所述语法错误。

Change:改变:

                {{ %s }} {{ "," if not loop.last }}

to:到:

                %s{{ ", " if not loop.last }}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM