Skip to main content

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

6.2.0 (2024-01-25)

New features:

  • Pull #13: ADD SQL Server style named parameter.

6.1.0 (2024-08-17)

New features:

  • Issue #11/Pull #12: Support Oracle named parameters enclosed in double quotes (style named_oracle).

Improvements:

  • Support Python 3.13.

6.0.1 (2023-12-09)

  • Fix documentation.

6.0.0 (2023-12-09)

  • Dropped support of EOL Python 3.7.

  • Support Python 3.12.

5.1.0 (2023-03-14)

Improvements:

5.0.0 (2022-08-11)

4.0.0 (2022-06-06)

  • Drop support for EOL Python 3.5.

  • Issue #10: When converting to ‘format’/’pyformat’ types, escape existing ‘%’ characters.

  • When converting from ‘format’/’pyformat’ types, set escape_char=True to unescape double ‘%’ characters.

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.

Metadata

Release files for sqlparams 6.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sqlparams 6.2.0
File Size Uploaded
sqlparams-6.2.0.tar.gz 34.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for sqlparams 6.2.0
File Interpreter ABI Platform
sqlparams-6.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 52.6 kB

Release files / sqlparams-6.2.0.tar.gz

Download URL sqlparams-6.2.0.tar.gz
Size 34.9 kB
Tags Source
SHA-256 checksum
How to use checksums
3744a2ad16f71293db6505b21fd5229b4757489a9b09f3553656a1ae97ba7ca5
BLAKE2b-256 checksum
How to use checksums
76ec5d6a5ca217ecd7b08d404b7dc2025c752bdb393c9b34fcc6d48e1f70bb7e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.11.11

Release files / sqlparams-6.2.0-py3-none-any.whl

Download URL sqlparams-6.2.0-py3-none-any.whl
Size 17.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
63b32ed9051bdc52e7e8b38bc4f78aed51796cdd9135e730f4c6a7db1048dedf
BLAKE2b-256 checksum
How to use checksums
97e2f1355629bb1eeb274babc947e2ba4e2e49250e934c86adcce3e54943bc8a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.11.11

Release history Release notifications | RSS feed

This release

6.2.0 This release

2 release files

6.1.0

2 release files

6.0.1

2 release files

6.0.0

2 release files

5.1.0

2 release files

5.0.0

2 release files

4.0.0

2 release files

3.0.0

2 release files

2.0.0

1 release file

1.2.0

1 release file

1.1.2

1 release file

1.1.1

1 release file

1.1.0

1 release file

1.0.3

1 release file

1.0.2

1 release file

1.0.1

1 release file

1.0.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page