andialbrecht

sqlparse: Tokenize, Format, and Inspect SQL in Python

sqlparse is a non-validating Python library for splitting SQL scripts, formatting queries, and inspecting token trees. It suits tools that need to work with varied SQL without enforcing a particular dialect.

View on GitHub Compare
Stars
4k
Forks
759
Stars, last 30 days
+0

Our take

Health score

41/100

How it is scored
Activity
37
Community
67
Issues
31
Pull requests
30

+0 stars in the last 30 days

  • More contributors than 77% of the projects we track
  • Slower to close issues than 100% of the projects we track

Project history

Daily GitHub snapshots since Oct 7, 2026. History covers stars and forks; health signals are not tracked historically.

Selected snapshot

Stars

4,022

Forks

782

Change since Nov 16, 2026: 0 stars·+1 forks

Overview

sqlparse turns SQL text into statements and token groups without checking whether the SQL is valid or enforcing a specific dialect. This makes it useful as a parsing layer for applications that need to handle SQL text, including vendor extensions or templates, rather than execute or validate queries.

It provides formatting and statement-splitting helpers alongside a traversable token tree. Python developers can use it directly or through the sqlformat command-line tool when building formatters, editors, linters, or query analysis utilities.

Key Features

  • Split a SQL script into individual statements.
  • Format SQL with options such as reindentation, keyword casing, comment stripping, and indentation width.
  • Parse statements into tokens and grouped structures that can be traversed.
  • Inspect statement types and nested constructs such as subqueries and CASE expressions.
  • Use the sqlformat command-line tool to format input from a file or standard input.
  • Integrate SQL formatting into commits with the provided pre-commit hook.
  • Process SQL without selecting a particular dialect or requiring the input to be valid.

Use Cases

  • Python developers can add SQL formatting to editor integrations or development utilities without implementing tokenization themselves.
  • Teams can standardize SQL layout in repositories using the formatter or pre-commit hook.
  • Linters and query-analysis tools can traverse statements and token groups to build their own checks.
  • Applications that process templated or vendor-specific SQL can tokenize text without first requiring dialect validation.

Getting Started

Install with pip install sqlparse. See the README for usage examples, command-line options, and documentation links.

Considerations

  • This is a non-validating parser. It does not establish that SQL is syntactically valid or suitable for execution.
  • The project makes no assumptions about a particular SQL dialect, so dialect-specific validation or semantic analysis must be handled separately.
  • The maintainers note that the project is maintained in spare time, and issue or pull request responses may take a while.

Found this useful?

Share it with someone who would like sqlparse.

Comparisons