【python】解析sql语句使用的表名

脚本内容如下:

import ply.lex as lex, re def extract_table_name_from_sql(sql_str): # remove the /* */ comments q = re.sub(r"/\*[^*]*\*+(?:[^*/][^*]*\*+)*/", "", sql_str) # remove whole line -- and # comments lines = [line for line in q.splitlines() if not re.match("^\s*(--|#)", line)] # remove trailing -- and # comments q = " ".join([re.split("--|#", line)[0] for line in lines]) # split on blanks, parens and semicolons tokens = re.split(r"[\s)(;]+", q) # scan the tokens. if we see a FROM or JOIN, we set the get_next # flag, and grab the next one (unless it's SELECT). result = set() get_next = False for token in tokens: if get_next: if token.lower() not in ["", "select"]: result.add(token) get_next = False get_next = token.lower() in ["from", "join"] return result sql2="SELECT a.time_updated_server/1000,content,nick,name FROM " \ "table1 a JOIN " \ "table2 b ON a.sender_id = b.user_id JOIN table3 c ON a.channel_id = c.channel_id JOIN table4 d ON c.store_id = d.store_id WHERE sender_id NOT IN(SELECT user_id FROM table5 WHERE store_id IN ('agent_store:1', 'ask:1')) AND to_timestamp(a.time_updated_server/1000)::date >= '2014-05-01' GROUP BY 1,2,3,4 HAVING sum(1) > 500 ORDER BY 1 ASC" print(extract_table_name_from_sql(sql2))

本文转自:
https://blog.csdn.net/onwingsofsong/article/details/83546768?utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-7.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-7.control


__EOF__

本文作者彬在俊
本文链接https://www.cnblogs.com/erlou96/p/16878338.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   彬在俊  阅读(236)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
点击右上角即可分享
微信分享提示