All Projects → cpburnz → python-sql-parameters

cpburnz / python-sql-parameters

Licence: MIT license
Convert DB API 2.0 named parameters to ordinal parameters.

Programming Languages

python
139335 projects - #7 most used programming language

Labels

Projects that are alternatives of or similar to python-sql-parameters

Sql Template Tag
ES2015 tagged template string for preparing SQL statements, works with `pg` and `mysql`
Stars: ✭ 132 (+340%)
Mutual labels:  sql-query
NLIDB
Natural Language Interface to DataBases
Stars: ✭ 100 (+233.33%)
Mutual labels:  sql-query
SqlFun
Idiomatic data access for F#
Stars: ✭ 33 (+10%)
Mutual labels:  sql-query
Sublimetext Sqltools
SQLTools for Sublime Text 3
Stars: ✭ 166 (+453.33%)
Mutual labels:  sql-query
SQL-Practice
Solutions to Problems For SQL on Leetcode, Hackerrank & DataLemur
Stars: ✭ 116 (+286.67%)
Mutual labels:  sql-query
SQLServerTools
This repo is the home of various SQL-Server-Tools
Stars: ✭ 28 (-6.67%)
Mutual labels:  sql-query
Ship Hold
data access framework for Postgresql on nodejs
Stars: ✭ 110 (+266.67%)
Mutual labels:  sql-query
SCCM-Zone
My ♡ collection of PowerShell scripts and SCCM related stuff :)
Stars: ✭ 73 (+143.33%)
Mutual labels:  sql-query
AndroidEasySQL-Library
An Easier & Lazier approach to SQL database for Android
Stars: ✭ 28 (-6.67%)
Mutual labels:  sql-query
Dev.Data
The Dev.Data.SqlDatabaseCommand is a set of components helping C# developers to execute SQL Queries and to retrieve data from SQL Server.
Stars: ✭ 15 (-50%)
Mutual labels:  sql-query
Join Monster
A GraphQL to SQL query execution layer for query planning and batch data fetching.
Stars: ✭ 2,395 (+7883.33%)
Mutual labels:  sql-query
Database
💾 A database layer with a familiar PDO-like API but much more powerful. Building queries, advanced joins, drivers for MySQL, PostgreSQL, SQLite, MS SQL Server and Oracle.
Stars: ✭ 251 (+736.67%)
Mutual labels:  sql-query
awesome-sql-builder
A small library for building SQL queries in a better way than regular string concatenation.
Stars: ✭ 44 (+46.67%)
Mutual labels:  sql-query
Querybuilder
SQL query builder, written in c#, helps you build complex queries easily, supports SqlServer, MySql, PostgreSql, Oracle, Sqlite and Firebird
Stars: ✭ 2,111 (+6936.67%)
Mutual labels:  sql-query
sqliteweb
Web-Based SQLite database browser
Stars: ✭ 93 (+210%)
Mutual labels:  sql-query
Sqlbuilder
A powerful, fast, cross-platform SQL Builder for PHP. Convert your structured data into SQL queries with a fluent style interface and targeting on all the mainstream database (MySQL, PostgreSQL, SQLite)
Stars: ✭ 131 (+336.67%)
Mutual labels:  sql-query
qhs
SQL queries on CSV and TSV files
Stars: ✭ 31 (+3.33%)
Mutual labels:  sql-query
soar-php
SQL optimizer and rewriter. - SQL 优化、重写器(辅助 SQL 调优)。
Stars: ✭ 140 (+366.67%)
Mutual labels:  sql-query
SQLiteQueryServer
Bulk query SQLite database over the network
Stars: ✭ 48 (+60%)
Mutual labels:  sql-query
secondary
Redis Secondary Indexing Module, been suspended see: https://github.com/RediSearch/RediSearch/
Stars: ✭ 33 (+10%)
Mutual labels:  sql-query

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].