Search code examples
pythonsqlparsingformatindentation

Python sqlparse doesn't create new line for each column properly


The common package for parsing SQL in Python is sqlparse:

pip3 install --user sqlparse

I wish to parse a list of create table statements, the library creates new lines sometimes but with wrong indentation:

import sqlparse;

print(sqlparse.format("create table (id int,foo text, bar float)", reindent=True, keyword_case="upper"));

The indentation is more and more for every subsequent line, this makes the SQL text look broken. How to tell sqlparse to indent properly? Tried reindent_aligned=True too but not working.


Solution

  • You can try my library SQLGlot

    python -m sqlglot "create table x (id int,foo text, bar float)"
    
    CREATE TABLE x (
      "id" INT,
      "foo" TEXT,
      "bar" FLOAT 
    )