Skip to main content

Convert between various DB API 2.0 parameter styles.

Project description

SQL Params

sqlparams is a utility package for converting between various SQL parameter styles. This can simplify the use of SQL parameters in queries by allowing the use of named parameters where only ordinal are supported. Some Python DB API 2.0 compliant modules only support the ordinal qmark or format style parameters (e.g., pyodbc only supports qmark). This package provides a helper class, SQLParams, that is used to convert from any parameter style (qmark, numeric, named, format, pyformat; and the non-standard numeric_dollar and named_dollar), and have them safely converted to the desired parameter style.

Tutorial

You first create an SQLParams instance specifying the named parameter style you’re converting from, and what ordinal style you’re converting to. Let’s convert from named to qmark style:

>>> import sqlparams
>>> query = sqlparams.SQLParams('named', 'qmark')

Now, lets to convert a simple SQL SELECT query using the SQLParams.format method which accepts an SQL query, and a dict of parameters:

>>> sql, params = query.format('SELECT * FROM users WHERE name = :name;', {'name': "Thorin"})

This returns the new SQL query using ordinal qmark parameters with the corresponding list of ordinal parameters, which can be passed to the .execute() method on a database cursor:

>>> print sql
SELECT * FROM users WHERE name = ?;
>>> print params
['Thorin']

tuples are also supported which allows for safe use of the SQL IN operator:

>>> sql, params = query.format("SELECT * FROM users WHERE name IN :names;", {'names': ("Dori", "Nori", "Ori")})
>>> print sql
SELECT * FROM users WHERE name in (?,?,?);
>>> print params
['Dori', 'Nori', 'Ori']

You can also format multiple parameters for a single, shared query useful with the .executemany() method of a database cursor:

>>> sql, manyparams = query.formatmany("UPDATE users SET age = :age WHERE name = :name;", [{'name': "Dwalin", 'age': 169}, {'name': "Balin", 'age': 178}])
>>> print sql
UPDATE users SET age = ? WHERE name = ?;
>>> print manyparams
[[169, 'Dwalin'], [178, 'Balin']]

Please note that if an expanded tuple is used in SQLParams.formatmany, the tuple must be the same size in each of the parameter lists. Otherwise, you might well use SQLParams.format in a for-loop.

Source

The source code for sqlparams is available from the GitHub repo cpburnz/python-sql-parameters.

Installation

sqlparams can be installed from source with:

python setup.py install

sqlparams is also available for install through PyPI:

pip install sqlparams

Documentation

Documentation for sqlparams is available on Read the Docs.

Change History

3.0.0 (2020-04-04)

  • Major changes to internal implementation.

  • Support converting any parameter style to any parameter style (all named, numeric, and ordinal styles).

  • Renamed attribute named to in_style on sqlparams.SQLParams.

  • Renamed attribute ordinal to out_style on sqlparams.SQLParams.

  • Removed attributes match and replace from sqlparams.SQLParams which should have been private.

  • Named parameters must now be valid identifiers (can no longer start with a digit to help prevent incorrectly matching common strings such as datetimes). Fixes Issue #4.

  • Issue #7: Support dollar sign style for numeric and named parameters: ``

2.0.0 (2020-02-26)

  • Drop support for EOL Python 2.7, 3.2, 3.3, 3.4.

1.2.0 (2020-02-26)

  • Require setuptools.

  • Support up to Python 3.8.

1.1.2 (2018-05-04)

  • Improved support for byte strings.

1.1.1 (2017-09-07)

  • Fixed support for byte strings.

1.1.0 (2017-08-30)

  • Support Python 3.2+.

1.0.3 (2012-12-28)

1.0.2 (2012-12-22)

  • Added sphinx documentation.

1.0.1 (2012-12-20)

  • Fixed running test as a script.

1.0.0 (2012-12-20)

  • Initial release.

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

sqlparams-3.0.0.tar.gz (27.8 kB view details)

Uploaded Source

Built Distribution

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

sqlparams-3.0.0-py3-none-any.whl (13.5 kB view details)

Uploaded Python 3

File details

Details for the file sqlparams-3.0.0.tar.gz.

File metadata

  • Download URL: sqlparams-3.0.0.tar.gz
  • Upload date:
  • Size: 27.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Python-urllib/3.6

File hashes

Hashes for sqlparams-3.0.0.tar.gz
Algorithm Hash digest
SHA256 2ad27aab848e9b678c70d3fd2aa0c2aa486a22c4d2e492e28051455704218d6b
MD5 38d86ada7464e74e78ede3b11fd32b6c
BLAKE2b-256 26e71718b2e8a6622662412ba8d0f5b4ad5fd43c8079980f8810b1462b3d043d

See more details on using hashes here.

File details

Details for the file sqlparams-3.0.0-py3-none-any.whl.

File metadata

  • Download URL: sqlparams-3.0.0-py3-none-any.whl
  • Upload date:
  • Size: 13.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Python-urllib/3.6

File hashes

Hashes for sqlparams-3.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3ced99c6be7dd9d9af64c87a28e89d8e180e90194156bb2f31113791ffe693e7
MD5 80dcfd33ff6878d51ea576dcb704e820
BLAKE2b-256 1b72e01570409286e92f19280beb0dc3f225198faad3d6efdce3268279977196

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