john-bodley / sqlparse

A non-validating SQL parser module for Python

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

python-sqlparse - Parse SQL statements

buildstatus coverage

sqlparse is a non-validating SQL parser for Python. It provides support for parsing, splitting and formatting SQL statements.

The module is compatible with Python 2.7 and Python 3 (>= 3.4) and released under the terms of the New BSD license.


Support for Python<3.4 (including 2.x) will be dropped soon.

Visit the project page at for further information about this project.

Quick Start

$ pip install sqlparse
>>> import sqlparse

>>> # Split a string containing two SQL statements:
>>> raw = 'select * from foo; select * from bar;'
>>> statements = sqlparse.split(raw)
>>> statements
['select * from foo;', 'select * from bar;']

>>> # Format the first statement and print it out:
>>> first = statements[0]
>>> print(sqlparse.format(first, reindent=True, keyword_case='upper'))
FROM foo;

>>> # Parsing a SQL statement:
>>> parsed = sqlparse.parse('select * from foo')[0]
>>> parsed.tokens
[<DML 'select' at 0x7f22c5e15368>, <Whitespace ' ' at 0x7f22c5e153b0>, <Wildcard '*' … ]


Project page
Bug tracker
Online Demo

sqlparse is licensed under the BSD license.

Parts of the code are based on pygments written by Georg Brandl and others. pygments-Homepage:

ezoic increase your site revenue


A non-validating SQL parser module for Python

License:BSD 3-Clause "New" or "Revised" License


Language:Python 93.7%Language:TSQL 4.4%Language:PLpgSQL 1.6%Language:Makefile 0.2%