| single |
# sqlparse
[Build Status]
[Coverage]
[Documentation]
[PyPI]
[Python versions]
[License]
> [!IMPORTANT]
> **Project Status: End of Maintenance**
>
> sqlparse is no longer actively maintained. Version 0.6.1 was its final
release.
>
> No further bug fixes, feature updates, or security fixes are planned. The
repository and published packages will remain available, but users should
not expect ongoing maintenance or security support.
>
> Software Supply Chain Security: To minimize supply chain risks, I will
retain control of the sqlparse project on PyPI and this GitHub repository.
There are no plans to transfer ownership or grant publishing or commit
access to third parties.
>
> After many years of maintaining sqlparse as a volunteer, I have decided
to bring the project to a close. A more detailed explanation of this
decision is available separately.
>
> THANKS TO ALL USERS AND CONTRIBUTORS!
> I would like to thank everyone who has contributed to sqlparse over the
years.
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
`bash
pip install sqlparse
`
## Usage
Three module-level functions cover most needs: `split()`, `format()` and
`parse()`.
### Split a script into statements
```python
import sqlparse
sqlparse.split("select * from foo; select * from bar;")
# ['select * from foo;', 'select * from bar;']
```
### Format a statement
```python
print(sqlparse.format("select id,name from users where active=1",
reindent=True, keyword_case="upper"))
`
`sql
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
```python
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
|