Skip to main content

sqlparse

Build Status Coverage Documentation PyPI Python versions License

A non-validating SQL parser for Python. Split scripts into statements, format them, and walk their token tree.

sqlparse tokenizes SQL text and groups the parts it recognizes into a tree of statements, clauses, identifiers and expressions. It accepts any input without validating it, and makes no assumptions about a particular SQL dialect, so vendor extensions and templated SQL parse too. It is a building block for formatters, linters, editors and query analysis tools.

Licensed under the New BSD license.

Install

pip install sqlparse

Usage

Three module-level functions cover most needs: split(), format() and parse().

Split a script into statements

import sqlparse

sqlparse.split("select * from foo; select * from bar;")
# ['select * from foo;', 'select * from bar;']

Format a statement

print(sqlparse.format("select id,name from users where active=1",
                      reindent=True, keyword_case="upper"))
SELECT id,
       name
FROM users
WHERE active=1

format() accepts the same options as the command line, among them reindent, keyword_case, strip_comments and indent_width.

Parse and inspect a statement

statement = sqlparse.parse("select id, name from users where active = 1")[0]

print(statement.get_type())
for token in statement.tokens:
    if not token.is_whitespace:
        print(f"{token.ttype or type(token).__name__!s:20} {token!s}")
SELECT
Token.Keyword.DML    select
IdentifierList       id, name
Token.Keyword        from
Identifier           users
Where                where active = 1

Tokens with a ttype are leaves. The others are groups you can descend into, which is how nested constructs like subqueries and CASE expressions are represented. See Analyzing SQL statements for the traversal API.

Command line

Installing sqlparse also provides the sqlformat command. It reads from stdin when the filename is -, writes to stdout by default, and rewrites files in place with --in-place:

sqlformat --reindent --keywords upper query.sql

Run sqlformat --help for all formatting options.

Pre-commit hook

Format SQL files on commit with pre-commit:

repos:
  - repo: https://github.com/andialbrecht/sqlparse
    rev: 0.5.5  # use the latest release
    hooks:
      - id: sqlformat
        args: [--in-place, --reindent, --keywords, upper]

The hook defaults to --in-place --reindent. When overriding args, keep --in-place, or the hook writes to stdout and leaves your files unchanged.

Links

Documentation https://sqlparse.readthedocs.io/
Release notes https://sqlparse.readthedocs.io/en/latest/changes.html
Issues https://github.com/andialbrecht/sqlparse/issues
Discussions https://github.com/andialbrecht/sqlparse/discussions
Online demo https://sqlformat.org/

Contributing

Contributions are welcome. See CONTRIBUTING.md to get started.

sqlparse is maintained in spare time, so it can take a while before an issue or pull request gets a reply. Thanks for your patience. A pull request that comes with tests for the behavior it changes is usually the quickest to review.

For security issues, please follow SECURITY.md rather than opening a public issue.

License

sqlparse is licensed under the New BSD license; see LICENSE for the full text. Parts of the code are based on Pygments, written by Georg Brandl and others.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sqlparse-0.6.0.tar.gz (178.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sqlparse-0.6.0-py3-none-any.whl (50.1 kB view details)

Uploaded Python 3

File details

Details for the file sqlparse-0.6.0.tar.gz.

File metadata

  • Download URL: sqlparse-0.6.0.tar.gz
  • Upload date:
  • Size: 178.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Hatch/1.18.0 {"ci":null,"cpu":"arm64","distro":{"name":"macOS","version":"26.6.1"},"implementation":{"name":"CPython","version":"3.11.14"},"installer":{"name":"hatch","version":"1.18.0"},"openssl_version":"OpenSSL 3.5.4 30 Sep 2025","python":"3.11.14","system":{"name":"Darwin","release":"25.6.0"}} HTTPX2/2.10.0

File hashes

Hashes for sqlparse-0.6.0.tar.gz
Algorithm Hash digest
SHA256 113c35c75365ab9cc9c7231d68c6428fb11c085fc8e9eb1ad659b7ddbf6cd2b9
MD5 900c83341958cb0a16380b204f2fd748
BLAKE2b-256 5fd33f06a1006f2261d1342aefb3c71eed02f5d4ca5bdbecd86ebc12ad38306e

See more details on using hashes here.

File details

Details for the file sqlparse-0.6.0-py3-none-any.whl.

File metadata

  • Download URL: sqlparse-0.6.0-py3-none-any.whl
  • Upload date:
  • Size: 50.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Hatch/1.18.0 {"ci":null,"cpu":"arm64","distro":{"name":"macOS","version":"26.6.1"},"implementation":{"name":"CPython","version":"3.11.14"},"installer":{"name":"hatch","version":"1.18.0"},"openssl_version":"OpenSSL 3.5.4 30 Sep 2025","python":"3.11.14","system":{"name":"Darwin","release":"25.6.0"}} HTTPX2/2.10.0

File hashes

Hashes for sqlparse-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b861c0288ce2fa56209a9a6412d2e066ac664b3873b89c26c9d8415e8e32996f
MD5 3771c8d55abedf8f34e0d9c60c6d1f2a
BLAKE2b-256 d950f00935da0ec7cbf325f8dc4f772ae46fbc7b672dd62876e73f0a94adda57

See more details on using hashes here.

Supported by

Image AWS Cloud computing and Security Sponsor Image Datadog Monitoring Image Depot Continuous Integration Image Fastly CDN Image Google Download Analytics Image Sentry Error logging Image StatusPage Status page