sql-smith is an SQL query builder with zero dependencies and a fluent interface.
Project description
sql-smith is an SQL query builder with zero dependencies and a fluent interface.
The sentence above is, beside the name, a copy from the website of the PHP library Latitude, for the simple reason that this Python module is a port of Latitude.
Installation
$ pip install sql-smith
Quick Start
QueryFactory is a factory to create a SELECT, INSERT, UPDATE or DELETE query. Use the fluent interface of the queries to complete the query.
from sql_smith import QueryFactory
from sql_smith.engine import CommonEngine
from sql_smith.functions import field
factory = QueryFactory(CommonEngine())
query = factory \
.select('id', 'username') \
.from_('users') \
.where(field('id').eq(5)) \
.compile()
print(query.sql) # SELECT "id", "username" FROM "users" WHERE "id" = ?
print(query.params) # (5)
When the query is ready, compile it. The return value of compile is a Query class instance with two properties: sql and params. Use these properties to pass the query to a database.
import sqlite3
db = sqlite3.connect('test.db')
cur = db.cursor()
for row in cur.execute(query.sql, query.params):
print(row)
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for sql_smith-1.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e5a2ccadb3d5b990380a7054e56d1e1826b9bd89415fe486788befefb314c375 |
|
MD5 | 9c16f29c82649d27e2f09d0e2daea347 |
|
BLAKE2b-256 | bf7fc7f0c5778974e6c8d50f394c743a52c3336c4f24426686af0b117b68b33d |