{"name":"sqlparse: A Powerful Non-Validating SQL Parser for Python","description":"sqlparse is a robust, non-validating SQL parser module for Python, designed to tokenize SQL text and group its components into a structured tree. It serves as a foundational tool for building formatters, linters, and query analysis applications. Developers can leverage its capabilities to split SQL scripts, format statements, and deeply inspect their token trees.","github":"https://github.com/andialbrecht/sqlparse","url":"https://osrepos.com/repo/andialbrecht-sqlparse","source":"osrepos.com","sourceDescription":"This repository profile is provided by osrepos.com, an open source repository discovery platform.","repositoryProfile":"https://osrepos.com/repo/andialbrecht-sqlparse","generatedFor":"open source discovery and AI-assisted research","markdown":"https://osrepos.com/repo/andialbrecht-sqlparse.md","json":"https://osrepos.com/repo/andialbrecht-sqlparse.json","topics":["Python","SQL","SQL Parser","SQL Formatter","Tokenizer","Code Quality","Development","Open Source"],"keywords":["Python","SQL","SQL Parser","SQL Formatter","Tokenizer","Code Quality","Development","Open Source"],"stars":null,"summary":"sqlparse is a robust, non-validating SQL parser module for Python, designed to tokenize SQL text and group its components into a structured tree. It serves as a foundational tool for building formatters, linters, and query analysis applications. Developers can leverage its capabilities to split SQL scripts, format statements, and deeply inspect their token trees.","content":"## Introduction\n\n`sqlparse` is a non-validating SQL parser for Python, offering powerful capabilities to process SQL text. It excels at splitting scripts into individual statements, formatting them according to various styles, and allowing users to traverse their intricate token trees.\n\nThis library tokenizes SQL text, organizing the recognized parts into a hierarchical structure of statements, clauses, identifiers, and expressions. A key advantage of `sqlparse` is its flexibility: it accepts any input without validation and makes no assumptions about specific SQL dialects. This makes it highly adaptable for handling vendor extensions and templated SQL, serving as an essential building block for formatters, linters, editors, and advanced query analysis tools.\n\n## Installation\n\nGetting started with `sqlparse` is straightforward. You can install it using pip:\n\nbash\npip install sqlparse\n\n\n## Examples\n\n`sqlparse` provides three primary module-level functions to cover most common needs: `split()`, `format()`, and `parse()`.\n\n### Split a script into statements\n\nEasily break down a SQL script containing multiple statements into a list of individual statements:\n\npython\nimport sqlparse\n\nsqlparse.split(\"select * from foo; select * from bar;\")\n# ['select * from foo;', 'select * from bar;']\n\n\n### Format a statement\n\nEnhance the readability of your SQL queries with `sqlparse`'s formatting capabilities. You can specify options like reindentation and keyword casing:\n\npython\nprint(sqlparse.format(\"select id,name from users where active=1\",\n                      reindent=True, keyword_case=\"upper\"))\n\n\nThis will produce:\n\nsql\nSELECT id,\n       name\nFROM users\nWHERE active=1\n\n\nThe `format()` function supports various options, including `reindent`, `keyword_case`, `strip_comments`, and `indent_width`, similar to its command-line counterpart.\n\n### Parse and inspect a statement\n\nFor deeper analysis, `sqlparse` allows you to parse a statement and inspect its token tree. This is invaluable for understanding the structure of complex queries:\n\npython\nstatement = sqlparse.parse(\"select id, name from users where active = 1\")[0]\n\nprint(statement.get_type())\nfor token in statement.tokens:\n    if not token.is_whitespace:\n        print(f\"{token.ttype or type(token).__name__!s:20} {token!s}\")\n\n\nOutput will resemble:\n\n\nSELECT\nToken.Keyword.DML    select\nIdentifierList       id, name\nToken.Keyword        from\nIdentifier           users\nWhere                where active = 1\n\n\nTokens with a `ttype` are leaf nodes, while others represent groups that can be further explored. This hierarchical representation is how nested constructs, such as subqueries and `CASE` expressions, are managed. For more details on traversing the API, refer to the [Analyzing SQL statements](https://sqlparse.readthedocs.io/en/latest/analyzing.html) documentation.\n\n## Why Use sqlparse?\n\n`sqlparse` stands out as an indispensable tool for anyone working with SQL in Python due to several key features:\n\n*   **Flexibility**: Its non-validating nature and dialect-agnostic approach mean it can parse virtually any SQL, including vendor-specific extensions and templated queries.\n*   **Foundation for Tools**: It provides the core parsing logic needed to build sophisticated SQL formatters, linters, editors, and advanced query analysis applications.\n*   **Simple API**: Common tasks like splitting, formatting, and parsing SQL are made easy with intuitive module-level functions.\n*   **Command-Line Utility**: The `sqlformat` command-line tool offers quick formatting directly from your terminal, supporting in-place file modifications.\n*   **Pre-commit Integration**: Seamlessly integrate `sqlparse` into your development workflow using pre-commit hooks to ensure consistent SQL formatting across your projects.\n\n## Links\n\nExplore `sqlparse` further with these official resources:\n\n*   Documentation: [https://sqlparse.readthedocs.io/](https://sqlparse.readthedocs.io/)\n*   Release notes: [https://sqlparse.readthedocs.io/en/latest/changes.html](https://sqlparse.readthedocs.io/en/latest/changes.html)\n*   Issues: [https://github.com/andialbrecht/sqlparse/issues](https://github.com/andialbrecht/sqlparse/issues)\n*   Discussions: [https://github.com/andialbrecht/sqlparse/discussions](https://github.com/andialbrecht/sqlparse/discussions)\n*   Online demo: [https://sqlformat.org/](https://sqlformat.org/)","metrics":{"detailViews":1,"githubClicks":0},"dates":{"published":null,"modified":"2026-07-29T23:45:01.000Z"}}