Skip to content

sqlparse.split

语法:

python
sqlparse.split(sql, encoding=None)

以列表的形式返回分割后的SQL语句,也可以通过指定encodiing参数来决定SQL的编码方式。

示例:

python
import sqlparse

sqlparse.format

语法:

python
sqlparse.format(sql, encoding=None, **options)

根据选项格式化SQL,也可以通过指定encodiing参数来决定SQL的编码方式,然后将格式化后的SQL语句返回。

来看示例:

python
import sqlparse

sql = "select id,age,name from staff where id=2;"
# 基本用法
format_sql = sqlparse.format(sql)
print(format_sql)
"""
select id,age,name from staff where id=2;
"""

# keyword_case:更改格式化后的SQL关键字的格式,允许值 upper、lower、capitalize
format_sql = sqlparse.format(sql, keyword_case='upper')
print(format_sql)
"""
SELECT id,age,name FROM staff WHERE id=2;
"""

# identifier_case:更改标识符的格式,允许值 upper、lower、capitalize
format_sql = sqlparse.format(sql, identifier_case='upper')
print(format_sql)
"""
select ID,AGE,NAME from STAFF where ID=2;
"""

# use_space_around_operators:如果为真,则在所有操作符周围使用空格。
format_sql = sqlparse.format(sql, use_space_around_operators=False)
print(format_sql)
format_sql = sqlparse.format(sql, use_space_around_operators=True)
print(format_sql)
"""
select id,age,name from staff where id=2;
select id,age,name from staff where id = 2;
"""

# reindent:如果为真,则修改语句的缩进
format_sql = sqlparse.format(sql, reindent=False)
print(format_sql)
format_sql = sqlparse.format(sql, reindent=True)
print(format_sql)
"""
select id,age,name from staff where id=2;
select id,
       age,
       name
from staff
where id=2;
"""

# reindent_aligned:如果为真,则修改语句的缩进,并按关键字对语句进行对齐。
format_sql = sqlparse.format(sql, reindent_aligned=False)
print(format_sql)
format_sql = sqlparse.format(sql, reindent_aligned=True)
print(format_sql)
"""
select id,age,name from staff where id=2;
select id,age,name from staff where id=2;
select id,
       age,
       name
  from staff
 where id=2;
"""

# strip_comments:True表示删除SQL中的注释
sql = "select id,age,name from staff where id=2;  -- 示例SQL"
format_sql = sqlparse.format(sql, strip_comments=False)
print(format_sql)
format_sql = sqlparse.format(sql, strip_comments=True)
print(format_sql)
"""
select id,age,name from staff where id=2;  -- 示例SQL
select id,age,name from staff where id=2;
"""

# use_space_around_operators:如果为真,则在所有操作符周围使用空格。
format_sql = sqlparse.format(sql, use_space_around_operators=False)
print(format_sql)
format_sql = sqlparse.format(sql, use_space_around_operators=True)
print(format_sql)
"""
select id,age,name from staff where id=2;
select id,age,name from staff where id = 2;
"""

# indent_tabs:如果为真,缩进使用制表符而不是空格
format_sql = sqlparse.format(sql, reindent=False, indent_tabs=True)
print(format_sql)
format_sql = sqlparse.format(sql, reindent=True, indent_tabs=True)
print(format_sql)
"""
select id,age,name from staff where id=2;
select id,
	age,
	name
from staff
where id=2;
"""

# output_format:如果给定,则对输出进行额外的格式化,以便作为编程语言中的变量使用。允许的值是"python"和"php"
format_sql = sqlparse.format(sql, output_format='php')
print(format_sql)
format_sql = sqlparse.format(sql, output_format='python')
print(format_sql)
"""
$sql = "select id,age,name from staff where id=2;";
sql = 'select id,age,name from staff where id=2;'
"""

更多参数说明参考:sqlparse– Parse SQL statements