sql
Why?
DB API 2.0 works. ORMs are convenient but sometimes overkill. sql is a lightweight wrapper sitting on top of any DB API 2.0 connection offering a postgres like interface which makes working with SQL results bliss.
How?
>>> import sqlite3
>>> connection = sqlite3.connect(':memory:')
>>> import sql >>> bliss = sql.SQL(connection)
run
run is the method to use when you want to run a query but do not care about the result e.g. to create a table:
>>> bliss.run("CREATE TABLE contributors (firstname VARCHAR, lastname VARCHAR)") #doctest: +ELLIPSIS
<sql.SQL object ...>
>>> bliss.run("INSERT INTO contributors VALUES (?, ?)", [('Andrew', 'Kuchling'),
... ('James', 'Henstridge'),
... ('Daniele', 'Varrazzo'),
... ('Marc-Andre', 'Lemburg')]) #doctest: +ELLIPSIS
<sql.SQL object ...>
Nothing impressive so far, creating a cursor and calling executemany would achieve the same result.
commit
Added in version 2022.4.0
>>> bliss.run("INSERT INTO contributors VALUES (?, ?)", ("Chad", "Whitacre")) #doctest: +ELLIPSIS
<sql.SQL object ...>
>>> bliss.commit()
Just because it is shorter than bliss.connection.commit()
run returns self so commit can be chained
>>> bliss.run("INSERT INTO contributors VALUES (?, ?)", ("Guido", "van Rossum")).commit()
one
one is the method to use when you know the result is a single row or only care about one.
>>> bliss.one("SELECT firstname FROM contributors WHERE lastname='Lemburg'") # doctest: +SKIP
u'Marc-Andre'
The string, nothing but the string, which in my book beats:
>>> cursor = connection.cursor()
>>> cursor.execute("SELECT firstname FROM contributors WHERE lastname='Lemburg'") # doctest: +ELLIPSIS
<sqlite3.Cursor object at ...>
>>> cursor.fetchone() # doctest: +SKIP
(u'Marc-Andre',)
Even better, if the result contains several column, one returns a namedtuple:
>>> bliss.one("SELECT * FROM contributors WHERE firstname='James'") # doctest: +SKIP
Record(firstname=u'James', lastname=u'Henstridge')
all
all is the method to use to retrieve all rows from a query.
>>> bliss.all("SELECT firstname FROM contributors") #doctest: +SKIP
[u'Andrew', u'James', u'Daniele', u'Marc-Andre']
It returns a list of namedtuples when appropriate:
>>> bliss.all("SELECT firstname, LENGTH(lastname) AS length FROM contributors") # doctest: +NORMALIZE_WHITESPACE +SKIP
[Record(firstname=u'Andrew', length=8),
Record(firstname=u'James', length=10),
Record(firstname=u'Daniele', length=8),
Record(firstname=u'Marc-Andre', length=7)]
Metadata
Release files for sql 2022.4.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| sql-2022.4.0.tar.gz | 4.2 kB | Without provenance | Details |
Release files / sql-2022.4.0.tar.gz
| Download URL | sql-2022.4.0.tar.gz |
|---|---|
| Size | 4.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f8cae71ac27e8bf763c4313497ba68f7e6ec577a1eb997738aaad3b3d5f7a600
|
|
BLAKE2b-256 checksum How to use checksums |
55c35698668c0d767bbca80d19b8c49c937e2ffc087e5a82c954c463400c4fd3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.8.0 pkginfo/1.8.2 readme-renderer/33.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.2 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.8.11
|
Security for sql 2022.4.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.