python3-sql解析库——sqlparse

1.官方文档

  https://sqlparse.readthedocs.io/en/latest/

2.快速开始

  使用pip或者conda安装:

 

conda install sqlparse

 

  使用官网示例快速入门,使用sqlparse的三大常用功能:

# -*- coding:UTF-8 -*-
import sqlparse

sql = "select id,name_,age from dual;select id,'18;19',age from actor;"
# 1.分割SQL
stmts = sqlparse.split(sql)
for stmt in stmts:
    # 2.format格式化
    print(sqlparse.format(stmt, reindent=True, keyword_case="upper"))
    # 3.解析SQL
    stmt_parsed = sqlparse.parse(stmt)
    print(stmt_parsed[0].tokens)

  输出如下:

SELECT id,
       name_,
       age
FROM dual;
[<DML 'select' at 0x20A7A0F3F48>, <Whitespace ' ' at 0x20A7A0FD5E8>, <IdentifierList 'id,nam...' at 0x20A7A0FA6D8>, <Whitespace ' ' at 0x20A7A0FD828>, <Keyword 'from' at 0x20A7A0FD888>, <Whitespace ' ' at 0x20A7A0FD8E8>, <Identifier 'dual' at 0x20A7A0FA660>, <Punctuation ';' at 0x20A7A0FD9A8>]
SELECT id,
       '18;19',
       age
FROM actor;
[<DML 'select' at 0x20A7A0F3B88>, <Whitespace ' ' at 0x20A7A0F3BE8>, <IdentifierList 'id,'18...' at 0x20A7A0FA7C8>, <Whitespace ' ' at 0x20A7A0F3E28>, <Keyword 'from' at 0x20A7A0F3E88>, <Whitespace ' ' at 0x20A7A0F3EE8>, <Identifier 'actor' at 0x20A7A0FA0C0>, <Punctuation ';' at 0x20A7A0F36A8>]

3.实例

 

转载于:https://www.cnblogs.com/jiangbei/p/11274942.html

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值