This module allows you to access a (DB API 2) SQL table using nothing but Python to build the query:
import re
import sqlite3
from simpleql.table import Table
conn = sqlite3.connect(":memory:")
curs = conn.cursor()
curs.execute("CREATE TABLE test (a integer, b char(1))")
curs.executemany("INSERT INTO test (a, b) VALUES (?, ?)", ([1,'a'], [2,'b'], [3,'c']))
conn.commit()
table = Table(conn, "test", verbose=1)
for row in table:
print row
This will print:
SELECT a, b FROM test;
{'a': 1, 'b': u'a'}
{'a': 2, 'b': u'b'}
{'a': 3, 'b': u'c'}
Note that each row in the table is a dictionary. We can filter this using a generator expression:
aspan = (1, 3)
for row in (t for t in table if min(aspan) < t['a'] < max(aspan)):
print row
This will print:
SELECT a, b FROM test WHERE (1<a) AND (a<3);
{'a': 2, 'b': u'b'}
As you can see, the query string is built from a generator expression. You can also use list comprehensions. Regular expressions are supported by the use of the re.search method:
filtered = [t for t in table if re.search('a', t['b'])]
print filtered
Which outputs:
SELECT a, b FROM test WHERE b LIKE "%a%";
[{'a': 1, 'b': u'a'}]
Note that since the module has to analyse the source code it doesn’t work on the interactive shell.
The advantage of this approach over the similar recipe is that if the (efficient) query builder fails when it encounters a complex filter the data will still be filtered (unefficiently) by the generator expression.
Metadata
Release files for simpleQL 0.2
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 | Actions |
|---|---|---|---|---|
| simpleQL-0.2.tar.gz | 5.6 kB | Without provenance | Details for simpleQL-0.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| simpleQL-0.2-py2.6.egg | Legacy Egg format | - | - | Details for simpleQL-0.2-py2.6.egg |
Total release size: 18.4 kB
Release files / simpleQL-0.2.tar.gz
| Download URL | simpleQL-0.2.tar.gz |
|---|---|
| Size | 5.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d6b1ec591068b1faa4500d12c76a71d04723cf5230f784dc3643b505090f6941
|
|
BLAKE2b-256 checksum How to use checksums |
06382cef04b5ee06a10fdf746f5a31b7fece84420b6de745ec6491830854763e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Release files / simpleQL-0.2-py2.6.egg
| Download URL | simpleQL-0.2-py2.6.egg |
|---|---|
| Size | 12.8 kB |
| Tags | Egg |
|
SHA-256 checksum How to use checksums |
93228658b1f39ff2f36681dec26367c228d98ea91cccf5eb9b5e1e9be7824f16
|
|
BLAKE2b-256 checksum How to use checksums |
71a684592535886f35b2140fc9510d7cff06c9a8659131929b4f5c44be0030a2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for simpleQL 0.2
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.