Skip to main content

A non-validating SQL parser.

Project description

buildstatus coverage docs packageversion

sqlparse is a non-validating SQL parser for Python. It provides support for parsing, splitting and formatting SQL statements.

The module is compatible with Python 3.8+ and released under the terms of the New BSD license.

Visit the project page at https://github.com/andialbrecht/sqlparse for further information about this project.

Quick Start

$ pip install sqlparse
>>> import sqlparse

>>> # Split a string containing two SQL statements:
>>> raw = 'select * from foo; select * from bar;'
>>> statements = sqlparse.split(raw)
>>> statements
['select * from foo;', 'select * from bar;']

>>> # Format the first statement and print it out:
>>> first = statements[0]
>>> print(sqlparse.format(first, reindent=True, keyword_case='upper'))
SELECT *
FROM foo;

>>> # Parsing a SQL statement:
>>> parsed = sqlparse.parse('select * from foo')[0]
>>> parsed.tokens
[<DML 'select' at 0x7f22c5e15368>, <Whitespace ' ' at 0x7f22c5e153b0>, <Wildcard '*'  ]
>>>

Pre-commit Hook

sqlparse can be used as a pre-commit hook to automatically format SQL files before committing:

repos:
  - repo: https://github.com/andialbrecht/sqlparse
    rev: 0.5.4  # Use the latest version
    hooks:
      - id: sqlformat
        # Optional: Add more formatting options
        # IMPORTANT: --in-place is required, already included by default
        args: [--in-place, --reindent, --keywords, upper]

Then install the hook:

$ pre-commit install

Your SQL files will now be automatically formatted on each commit.

Note: The hook uses --in-place --reindent by default. If you override the args, you must include --in-place for the hook to work.

Project details


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.5.4.tar.gz (120.1 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.5.4-py3-none-any.whl (45.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: sqlparse-0.5.4.tar.gz
  • Upload date:
  • Size: 120.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Hatch/1.16.1 cpython/3.12.11 HTTPX/0.28.1

File hashes

Hashes for sqlparse-0.5.4.tar.gz
Algorithm Hash digest
SHA256 4396a7d3cf1cd679c1be976cf3dc6e0a51d0111e87787e7a8d780e7d5a998f9e
MD5 b5bf6082e38fb99bf8217f429b73346a
BLAKE2b-256 1867701f86b28d63b2086de47c942eccf8ca2208b3be69715a1119a4e384415a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: sqlparse-0.5.4-py3-none-any.whl
  • Upload date:
  • Size: 45.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Hatch/1.16.1 cpython/3.12.11 HTTPX/0.28.1

File hashes

Hashes for sqlparse-0.5.4-py3-none-any.whl
Algorithm Hash digest
SHA256 99a9f0314977b76d776a0fcb8554de91b9bb8a18560631d6bc48721d07023dcb
MD5 5dc8ae30ccf48bbf535da552c5f56321
BLAKE2b-256 2570001ee337f7aa888fb2e3f5fd7592a6afc5283adb1ed44ce8df5764070f22

See more details on using hashes here.

Supported by

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