sqlite3
accepts qmark and named as parameter binding formats, but does not accept the formats and pyformat formats defined in PEP249 [^ 1] [^ 2] [^ 3]. This is a module that accepts them.
Python versions have been tested with 2.7.8 and 3.4.1.
pip install sqlite3paramstyle
It binds to the part of % s
.
conn = sqlite3paramstyle.connect(":memory:")
cur = conn.cursor()
cur.execute("select %s, %s", ("foo", "bar"))
cur.fetchall() # [("foo", "bar")]
It also supports the Python extended format.
cur.execute("select %(who)s, %(age)s", {"who": "John", "age": 20})
cur.fetchall() # [("John", 20)]$
I hope it helps someone somewhere: beers:
I was supposed to maintain Python code with almost no test code and added tests quietly, but I found Python code that uses psycopg2
live. I definitely wanted to use SQLAlchemy
, but since it was a difficult phase to change the dependent modules, I decided to proceed in the direction of not changing as much as possible.
I didn't want to keep the Postgresql process running on the CI server all the time, and the DB connection was in a form that could be passed from the outside, so when testing, I tried to pass the connection of sqlite3
and execute it.
However, the parameter styles that psycopg2
accepts are the format and pyformat formats described above, which are completely incompatible with sqlite3
.
I found qmarkpg that extends psycopg2
to accept qmark styles, so why not write a module that works the other way around? As soon as I thought.
Happy to be a PyPI author: smiley_cat: Wow Wow
Recommended Posts