Skip to main content

Simple SQL in Python.

Project description

aiosql

Simple SQL in Python

SQL is code, you should be able to write it, version control it, comment it, and run it using files. Writing your SQL code in Python programs as strings doesn't allow you to easily reuse your SQL in database GUI tools or CLI tools like psql. With aiosql you can organize your SQL statements in .sql files, load them into your python application as methods to call without losing the ability to use them as you would any other SQL file.

This project supports standard and asyncio based drivers for SQLite and PostgreSQL out of the box (sqlite3, aiosqlite, psycopg2, asyncpg). Extensions to support other database drivers can be written by you!

If you are using python versions <3.6 please see the related anosql package which this project is based on.

Documentation

Project and API docs https://nackjicholson.github.io/aiosql

Install

pip install aiosql

Or if you you use poetry:

poetry add aiosql

Usage

Given you have a SQL file like the one below called users.sql

-- name: get-all-users
-- Get all user records
select userid,
       username,
       firstname,
       lastname
  from users;


-- name: get-user-by-username^
-- Get user with the given username field.
select userid,
       username,
       firstname,
       lastname
  from users
 where username = :username;

You can use aiosql to load the queries in this file for use in your Python application:

import aiosql
import sqlite3

conn = sqlite3.connect("myapp.db")
queries = aiosql.from_path("users.sql", "sqlite3")

users = queries.get_all_users(conn)
# >>> [(1, "nackjicholson", "William", "Vaughn"), (2, "johndoe", "John", "Doe"), ...]

users = queries.get_user_by_username(conn, username="nackjicholson")
# >>> (1, "nackjicholson", "William", "Vaughn")

Writing SQL in a file and executing it from methods in python!

Why you might want to use this

  • You think SQL is pretty good, and writing SQL is an important part of your applications.
  • You don't want to write your SQL in strings intermixed with your python code.
  • You're not using an ORM like SQLAlchemy or Django, and you don't need to.
  • You want to be able to reuse your SQL in other contexts. Loading it into psql or other database tools.

Why you might NOT want to use this

  • You're looking for an ORM.
  • You aren't comfortable writing SQL code.
  • You don't have anything in your application that requires complicated SQL beyond basic CRUD operations.
  • Dynamically loaded objects built at runtime really bother you.

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

aiosql-3.1.1.tar.gz (11.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

aiosql-3.1.1-py3-none-any.whl (14.0 kB view details)

Uploaded Python 3

File details

Details for the file aiosql-3.1.1.tar.gz.

File metadata

  • Download URL: aiosql-3.1.1.tar.gz
  • Upload date:
  • Size: 11.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.5 CPython/3.8.5 Linux/5.4.55-1-lts

File hashes

Hashes for aiosql-3.1.1.tar.gz
Algorithm Hash digest
SHA256 8e1edd0c5a14fe048573880e6feab9b564a29f9b3a4449efba097be520a20250
MD5 279fd53d173e30707bbfdc8c812f0180
BLAKE2b-256 f87d1d345a5dcb8502dd3792aef4df0d55977a66da62ae660dd1c7ed4db9398a

See more details on using hashes here.

File details

Details for the file aiosql-3.1.1-py3-none-any.whl.

File metadata

  • Download URL: aiosql-3.1.1-py3-none-any.whl
  • Upload date:
  • Size: 14.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.5 CPython/3.8.5 Linux/5.4.55-1-lts

File hashes

Hashes for aiosql-3.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 cde8a04ca29ee900f77cc64469b490622975cc4c301d6f47e121bfabb7820f94
MD5 f5b59d40a23ad3436c441dd24e9c4da0
BLAKE2b-256 982062e359422884b6459331304872287fdb9370ba1bba74988966b423bf0134

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page