Skip to main content

Convert DB API 2.0 named parameters to ordinal parameters.

Project description

sqlparams: SQL Parameters

Introduction

sqlparams is a utility module for simplifying the use of SQL queries. Some Python DB API 2.0 compliant modules only support the ordinal qmark or format style parameters (e.g., pyodbc only supports qmark). This utility module provides a helper class, SQLParams, that is used to support named parameter styles such as named, numeric and pyformat, and have them safely converted to the desired ordinal style.

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 format() method which accepts an SQL query, and a dictionary 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 a tuple is used in formatmany(), the tuple must be the same size in each of the parameter lists. Otherwise, you might well use 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

Change History

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-1.0.0.tar.gz (6.5 kB view details)

Uploaded Source

File details

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

File metadata

  • Download URL: sqlparams-1.0.0.tar.gz
  • Upload date:
  • Size: 6.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for sqlparams-1.0.0.tar.gz
Algorithm Hash digest
SHA256 e5bb86ba3ab4e495038657ecec9e48e7d93bad74622ae2601baeb863b1a1036d
MD5 652641ea5d0f7005e4ebefb831c76cad
BLAKE2b-256 0644263f1efdfd48c5ce79056285d4d76903d659492cfa4629fd0c4640529a57

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