Skip to main content

A tiny library for submitting raw SQL via SQLAlchemy

Project description

sqla-raw

I've copy-pasted this enough times that it seemed worth packaging. Really not much more than a single method (raw.db.result) making it e-z to submit raw SQL to a database using a SQLAlchemy engine connection. Returns results as a list of dictionaries, with each dict keyed by column names. result_from_file is almost the same except it takes a path to read the query from.

Installation

pip install sqla-raw[pg]

Usage

Configure your database connection string by setting $DATABASE_URL in your environment.

>>> from raw import db
>>> x = db.result('select version()');
>>> x
[{'version': 'PostgreSQL 10.10 on x86_64-apple-darwin14.5.0, compiled by Apple LLVM version 7.0.0 (clang-700.1.76), 64-bit'}]

Because it's SQLAlchemy, you can safely use named parameters in your SQL string with colon-prepended :key format, and assign values in kwargs.

You can also use Jinja2 templating syntax to interpolate the query, if desired. (It uses a SandboxedEnvironment, but avoid this option with untrusted inputs, for obvious reasons.)

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

sqla-raw-0.3.1.tar.gz (4.2 kB view hashes)

Uploaded Source

Built Distribution

sqla_raw-0.3.1-py2.py3-none-any.whl (4.2 kB view hashes)

Uploaded Python 2 Python 3

Supported by

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