My favorites | Sign in
Project Home Downloads Wiki Issues Source
Project Information
Members
Featured
Downloads
Wiki pages
Links

sqlparse is a non-validating SQL parser for Python.

It provides support for parsing, splitting and formatting SQL statements.

sqlparse is released under the terms of the New BSD license.

Visit http://sqlformat.appspot.com to try out it's formatting functions.

Download & Installation

To download and install sqlparse on your system either download the latest released version using the "Downloads" tab above and run

python setup.py install

or install it with your favorite installer for Python modules using the Python Package Index.

Binary packages are available for Debian, Ubuntu, Fedora and ArchLinux (via AUR).

Click on the "Sources" tab above for instructions how to download the development version.

Documentation

Read the API documentation or an introductory article about this module: Part I, Part II.

Example Usage

Here are some usage examples of this module.

>>> # Splitting statements
>>> import sqlparse
>>> sqlparse.split('select * from foo; select * from bar;')
<<< [u'select * from foo; ', u'select * from bar;']

>>> # Formatting statemtents::
>>> print sqlparse.format('select * from foo where id in (select id from bar);',
...   reindent=True, keyword_case='upper')
SELECT *
FROM foo
WHERE id IN
  (SELECT id
   FROM bar);

>>> # Parsing
>>> res = sqlparse.parse('select * from "someschema"."mytable" where id = 1')
>>> res
<<< (<Statement 'select...' at 0x9ad08ec>,)
>>> stmt = res[0]
>>> stmt.to_unicode()  # converting it back to unicode
<<< u'select * from "someschema"."mytable" where id = 1'
>>> # This is how the internal representation looks like:
>>> stmt.tokens
<<<
(<DML 'select' at 0x9b63c34>,
 <Whitespace ' ' at 0x9b63e8c>,
 <Operator '*' at 0x9b63e64>,
 <Whitespace ' ' at 0x9b63c5c>,
 <Keyword 'from' at 0x9b63c84>,
 <Whitespace ' ' at 0x9b63cd4>,
 <Identifier '"somes...' at 0x9b5c62c>,
 <Whitespace ' ' at 0x9b63f04>,
 <Where 'where ...' at 0x9b5caac>)
>>>

Contributing

NOTE: Development moved to github: https://github.com/andialbrecht/sqlparse/*

Please file bug reports and feature requests on the project site at https://github.com/andialbrecht/sqlparse/issues/new or if you have code to contribute upload it to http://codereview.appspot.com and add albrecht.andi@googlemail.com as reviewer. For more information about the review tool and how to use it visit it's project page: http://code.google.com/p/rietveld

Powered by Google Project Hosting