How do I generate SQL code for a SQLite3 database somehow dynamically with something like a template engine? I'm new to this and I'm using jinja2 to generate HTML. Could jinja2 possibly be used for such a purpose as well, if so, is that a good idea? I would like my program to save all the data (attribute values of instances of various classes) before it stops running.
2 Answers
check out https://pypi.python.org/pypi/jinjasql/0.1.6 it seems to basically just apply jinja2 to SQL templates. I haven't played with it much yet but was just asking the same question and came upon this.
2 Comments
Ben Rudolph
Note there is a very dangerous infinite loop bug in this library: github.com/hashedin/jinjasql/pull/17
Sripathi Krishnan
For posterity, the infinite loop bug has been resolved, thanks @BenRudolph!
PugSQL is described as: a simple Python interface for using parameterized SQL, in files, with any SQLAlchemy-supported database.
It is a template engine in that parameters are passed into SQL, from which python functions are created. It is a powerful idea/approach borrowed from HugSQL and YesQL (Clojure implementations).
jsonmodule) would be easier?