sqlparse – Parse SQL statements¶
The sqlparse module provides the following functions on module-level.
- sqlparse.split(sql, encoding=None)¶
Split sql into single statements.
- Parameters:
sql – A string containing one or more SQL statements.
encoding – The encoding of the statement (optional).
- Returns:
A list of strings.
- sqlparse.format(sql, encoding=None, **options)¶
Format sql according to options.
Available options are documented in Formatting of SQL Statements.
In addition to the formatting options this function accepts the keyword “encoding” which determines the encoding of the statement.
- Returns:
The formatted SQL statement as string.
- sqlparse.parse(sql, encoding=None)¶
Parse sql and return a list of statements.
- Parameters:
sql – A string containing one or more SQL statements.
encoding – The encoding of the statement (optional).
- Returns:
A tuple of
Statementinstances.
In most cases there’s no need to set the encoding parameter. If encoding is not set, sqlparse assumes that the given SQL statement is encoded either in utf-8 or latin-1.
Formatting of SQL Statements¶
The format() function accepts the following keyword arguments.
keyword_caseChanges how keywords are formatted. Allowed values are “upper”, “lower” and “capitalize”.
identifier_caseChanges how identifiers are formatted. Allowed values are “upper”, “lower”, and “capitalize”.
strip_commentsIf
Truecomments are removed from the statements.truncate_stringsIf
truncate_stringsis a positive integer, string literals longer than the given value will be truncated.truncate_char(default: “[…]”)If long string literals are truncated (see above) this value will be append to the truncated string.
reindentIf
Truethe indentations of the statements are changed.indent_tabsIf
Truetabs instead of spaces are used for indentation.indent_widthThe width of the indentation, defaults to 2.
wrap_afterThe column limit for wrapping comma-separated lists. If unspecified, it puts every item in the list on its own line.
output_formatIf given the output is additionally formatted to be used as a variable in a programming language. Allowed values are “python” and “php”.