
我将为作业使用适当的工具
sqlparse-SQL解析器,获取第一个语句对象,并检查其
SELECt类型是否正确:
In [1]: import sqlparseIn [2]: queries = [ ...: "SELECT * FROM table", ...: """ ...: # SELECt * FROM table; ...: DROp TABLE table; ...: """, ...: """/* SELECT * FROM ...: TABLE /* DROp table table;""", ...: """ ...: # here is a comment ...: SELECT * FROM table ...: """ ...: ]In [3]: def is_select(query): first_statement = next((token for token in sqlparse.parse(query) if isinstance(token, sqlparse.sql.Statement)), None) if first_statement: return first_statement.get_type() == 'SELECT'In [4]: for query in queries: ...: print(query, is_select(query)) SELECT * FROM table True # SELECt * FROM table; DROp TABLE table; False /* SELECT * FROM TABLE /* DROp table table; False # here is a comment SELECT * FROM table True
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)