python sqlparse 各种 token

https://blog.csdn.net/qq_39607437/article/details/79620383

import sqlparse


def look(statement):
    print("statement: {}".format(statement))
    print("type: {}".format(type(statement)))
    for token in statement:
        print("{} ------ {} ----- {} ".format(type(token), token.ttype, token.value))
    print("
")
    return None

string_1 = "select * from history"
string_1_statement = sqlparse.parse(string_1)[0]
look(string_1_statement)

string_2 = "select age from history where id = 10"
string_2_statement = sqlparse.parse(string_2)[0]
look(string_2_statement)

string_3 = "select count(*) from history where age>10"
string_3_statment = sqlparse.parse(string_3)[0]
look(string_3_statment)

string_4 = "select count(age) from history where age>10 and gender='women'"
string_4_statement = sqlparse.parse(string_4)[0]
look(string_4_statement)

string_5 = "where id = 10"
string_5_statement = sqlparse.parse(string_5)[0]
look(string_5_statement)
print(string_5_statement.tokens)
print("
")
look(string_5_statement[0])
look(string_5_statement[0][2])

string_6 = "where age >10 or gender != woman"
string_6_statement = sqlparse.parse(string_6)[0]
look(string_6_statement)   # where 语句
look(string_6_statement[0])  # where 语句 是一种tokenlist


string_7 = "age >10"
string_7_statement = sqlparse.parse(string_7)[0]
look(string_7_statement)
look(string_7_statement[0])


string_8 = "max(age)>10"
string_8_statement = sqlparse.parse(string_8)[0]
look(string_8_statement)
look(string_8_statement[0])
look(string_8_statement[0][0])
look(string_8_statement[0][0][1])

string_9 = "(age<10 or gender=man) and (age>18 and gender=10)"
string_9_statement = sqlparse.parse(string_9)[0]
look(string_9_statement)
look(string_9_statement[0])

string_10 = "select name as rs from applicant where zhima>557 limit 100, 100;"
string_10_statement = sqlparse.parse(string_10)[0]
look(string_10_statement)

string_11 = "/* context_name=cut_age */ select count(*), gender from history where age>18 group by gender;"
string_11_statement = sqlparse.parse(string_11)[0]
look(string_11_statement)

string_12 = "select age from applicant where applicant.name=tom"
string_12_statement = sqlparse.parse(string_12)[0]
look(string_12_statement)

string_13 = "count(age)"
string_13_statement = sqlparse.parse(string_13)[0]
look(string_13_statement)
look(string_13_statement[0])

原文地址:https://www.cnblogs.com/jijizhazha/p/9129539.html