python sqlparse_Python sqlparse包_程序模块 - PyPI - Python中文网

sqlparse是一个未验证的sql解析器模块。

它提供了对sql语句的解析、拆分和格式化的支持。

访问project page以获取

其他信息和文件。

示例用法

拆分SQL语句:>>> import sqlparse

>>> sqlparse.split('select * from foo; select * from bar;')

[u'select * from foo; ', u'select * from bar;']

格式化语句:>>> sql = 'select * from foo where id in (select id from bar);'

>>> print sqlparse.format(sql, reindent=True, keyword_case='upper')

SELECT *

FROM foo

WHERE id IN

(SELECT id

FROM bar);

解析:>>> sql = 'select * from someschema.mytable where id = 1'

>>> res = sqlparse.parse(sql)

>>> res

(,)

>>> stmt = res[0]

>>> str(stmt) # converting it back to unicode

'select * from someschema.mytable where id = 1'

>>> # This is how the internal representation looks like:

>>> stmt.tokens

(,

,

,

,

,

,

,

,

)

欢迎加入QQ群-->: 979659372

推荐PyPI第三方库

你可能感兴趣的:(python,sqlparse)