Skip to content

Repository files navigation

sql-smith

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.

Read the full documentation.

Development of sql-smith will be continued in the uv worskpace of the kwai repository at Codeberg.

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.

fromsql_smithimportQueryFactoryfromsql_smith.engineimportCommonEnginefromsql_smith.functionsimportfieldfactory=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.

importsqlite3db=sqlite3.connect('test.db')
cur=db.cursor()
forrowincur.execute(query.sql, query.params):
print(row)

About

An SQL query builder with zero dependencies and a fluent interface

Topics

Resources

Stars

4 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages