sripathikrishnan / jinjasql

Template Language for SQL with Automatic Bind Parameter Extraction
MIT License
807 stars 88 forks source link

Docs: How to use JinjaSQL for parameterised inserts #48

Closed JimboMonkey closed 2 years ago

JimboMonkey commented 2 years ago

Hello,

I'm having trouble understanding how I should use JinjaSQL to make a template to insert values. For example, the following doesn't work. I know the problem is with my VALUES section, but I'm not sure how I should change it. Please could you help? Thank you

def add_entry(self, entry):
        add_entry_template = '''INSERT INTO
                                    cards(title,
                                          name,
                                          address,
                                          date_added)
                                VALUES(title,
                                       name,
                                       address,
                                       date_added)'''
        params = { 'title': entry.title,
                   'name': entry.name,
                   'address': entry.address,
                   'date_added': entry.date_added }
        cursor = self.execute_sql(add_entry_template, params)

def execute_sql(self, template, params):

        j = JinjaSql(param_style='named')
        query, bind_params = j.prepare_query(template, params)

        ...
JimboMonkey commented 2 years ago

I've worked it out. I need to add double curly brackets around each value:

VALUES({{title}},
       {{name}},
       {{address}},
       {{date_added}})