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.

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.

Metadata

Release files for sqlparse 0.6.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sqlparse 0.6.1
File Size Uploaded Provenance Actions
sqlparse-0.6.1.tar.gz 182.2 kB Without provenance Details for sqlparse-0.6.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for sqlparse 0.6.1
File Interpreter ABI Platform Actions
sqlparse-0.6.1-py3-none-any.whl Python 3 none any Details for sqlparse-0.6.1-py3-none-any.whl

Total release size: 233.4 kB

Release files / sqlparse-0.6.1.tar.gz

Download URL sqlparse-0.6.1.tar.gz
Size 182.2 kB
Tags Source
SHA-256 checksum
How to use checksums
ca722573f848ff2d30e40ea30f53a104b7dea7635ea9285891263eb2c58fb6e7
BLAKE2b-256 checksum
How to use checksums
7ee0361c764ea58cc1a75f7b4b424a74b08449db8bfa5e9199c7275999fdd1b9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via Hatch/1.18.0 {"ci":null,"cpu":"arm64","distro":{"name":"macOS","version":"27.2"},"implementation":{"name":"CPython","version":"3.14.6"},"installer":{"name":"hatch","version":"1.18.0"},"openssl_version":"OpenSSL 3.5.7 9 Jun 2026","python":"3.14.6","system":{"name":"Darwin","release":"27.2.0"}} HTTPX2/2.10.0

Release files / sqlparse-0.6.1-py3-none-any.whl

Download URL sqlparse-0.6.1-py3-none-any.whl
Size 51.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3be5a72d04c873d690d7c6dedd33d6fae2ef82759881ceec02fc2efd0cba564f
BLAKE2b-256 checksum
How to use checksums
dfff2fee788e82e75be8f210c65c5d103bf44c880e3769c884652a4cd911b8ba
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via Hatch/1.18.0 {"ci":null,"cpu":"arm64","distro":{"name":"macOS","version":"27.2"},"implementation":{"name":"CPython","version":"3.14.6"},"installer":{"name":"hatch","version":"1.18.0"},"openssl_version":"OpenSSL 3.5.7 9 Jun 2026","python":"3.14.6","system":{"name":"Darwin","release":"27.2.0"}} HTTPX2/2.10.0

Security for sqlparse 0.6.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.6.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page