3

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.

1 Answer 1

1

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 
)   
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.