Welcome, guest | Sign In | My Account | Store | Cart

Notice! PyPM is being replaced with the ActiveState Platform, which enhances PyPM’s build and deploy capabilities. Create your free Platform account to download ActivePython or customize Python with the packages you require and get automatic updates.

Download
ActivePython
INSTALL>
pypm install sqlparams

How to install sqlparams

  1. Download and install ActivePython
  2. Open Command Prompt
  3. Type pypm install sqlparams
 Python 2.7Python 3.2Python 3.3
Windows (32-bit)
Windows (64-bit)
Mac OS X (10.5+)
Linux (32-bit)
1.0.1
1.0.3Never BuiltWhy not?
1.0.1 Available View build log
Linux (64-bit)
1.0.3 Available View build log
1.0.1 Available View build log
 
License
MIT
Imports
Lastest release
version 1.0.3 on Jan 9th, 2014

sqlparams: SQL Parameters

sqlparams is a utility module for simplifying the use of SQL parameters in 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.

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 .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 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
Documentation

Documentation for sqlparams is available on Read the Docs.

Change History

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.

Subscribe to package updates

Last updated Jan 9th, 2014

What does the lock icon mean?

Builds marked with a lock icon are only available via PyPM to users with a current ActivePython Business Edition subscription.

Need custom builds or support?

ActivePython Enterprise Edition guarantees priority access to technical support, indemnification, expert consulting and quality-assured language builds.

Plan on re-distributing ActivePython?

Get re-distribution rights and eliminate legal risks with ActivePython OEM Edition.