My favorites | Sign in
Project Logo
                
Code license: New BSD License
Labels: SQL, Python, parser
Show all Featured downloads:
sqlparse-0.1.1.tar.gz
Show all Featured wiki pages:
KnownIssues
People details
Project owners:
  albrecht.andi

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.

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

Please file bug reports and feature requests on the project site at http://code.google.com/p/python-sqlparse/issues/entry 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









Hosted by Google Code