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.實例

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM