Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docs: Example for multi row insert statement (bulk insert) #47

Open
kishaningithub opened this issue Dec 28, 2021 · 2 comments
Open

Docs: Example for multi row insert statement (bulk insert) #47

kishaningithub opened this issue Dec 28, 2021 · 2 comments
Labels

Comments

@kishaningithub
Copy link

Would like to have an example which documents how to perform bulk insert

Desired output

INSERT INTO 
    links (url, name)
VALUES
    ('https://www.google.com','Google'),
    ('https://www.yahoo.com','Yahoo'),
    ('https://www.bing.com','Bing');
@sripathikrishnan
Copy link
Owner

Working Code:

from jinjasql import JinjaSql

urls = [
    ("google", "https://google.com"),
    ("yahoo", "https://yahoo.com"),
    ("fb", "https://facebook.com"),
    ("twitter", "https://twitter.com"),
]

template = '''
INSERT INTO 
    links(name, url)
VALUES
{% for url in urls %}
    ( {{url[0]}}, {{url[1]}} )
{% endfor %}
'''

j = JinjaSql(param_style="qmark")

query, bindparams = j.prepare_query(template, {"urls": urls})
print(query)
print(bindparams)

Should print this:

INSERT INTO 
    links(name, url)
VALUES

    (?, ?)

    (?, ?)

    (?, ?)

    (?, ?)

['google', 'https://google.com', 'yahoo', 'https://yahoo.com', 'fb', 'https://facebook.com', 'twitter', 'https://twitter.com']

@kishaningithub
Copy link
Author

@sripathikrishnan Thanks a ton! :-) Can i add this to readme or wiki?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants