简体   繁体   English

如何使用 Python 从 SQL 查询中提取列名

[英]How to extract column names from SQL query using Python

I would like to extract the column names of a resulting table directly from the SQL statement:我想直接从 SQL 语句中提取结果表的列名:


query = """

select 
    sales.order_id as id, 
    p.product_name, 
    sum(p.price) as sales_volume 
from sales
right join products as p 
    on sales.product_id=p.product_id
group by id, p.product_name;

"""

column_names = parse_sql(query)
# column_names:
# ['id', 'product_name', 'sales_volume']

Any idea what to do in parse_sql() ?知道在parse_sql()中要做什么吗? The resulting function should be able to recognize aliases and remove the table aliases/identifiers (eg "sales." or "p.").生成的 function 应该能够识别别名并删除表别名/标识符(例如“sales.”或“p.”)。

Thanks in advance!提前致谢!

I've done something like this using the library sqlparse .我已经使用库sqlparse做了类似的事情。 Basically, this library takes your SQL query and tokenizes it.基本上,这个库接受您的 SQL 查询并对其进行标记。 Once that is done, you can search for the select query token and parse the underlying tokens.完成后,您可以搜索 select 查询令牌并解析底层令牌。 In code, that reads like在代码中,这看起来像

import sqlparse
def find_selected_columns(query) -> list[str]:
    tokens = sqlparse.parse(query)[0].tokens
    found_select = False
    for token in tokens:
        if found_select:
            if isinstance(token, sqlparse.sql.IdentifierList):
                return [
                    col.value.split(" ")[-1].strip("`").rpartition('.')[-1]
                    for col in token.tokens
                    if isinstance(col, sqlparse.sql.Identifier)
                ]
        else:
            found_select = token.match(sqlparse.tokens.Keyword.DML, ["select", "SELECT"])
    raise Exception("Could not find a select statement. Weired query :)")

This code should also work for queries with Common table expressions, ie it only return the final select columns.此代码也适用于使用公用表表达式的查询,即它只返回最终的 select 列。 Depending on the SQL dialect and the quote chars you are using, you might to have to adapt the line col.value.split(" ")[-1].strip("`").rpartition('.')[-1]根据 SQL 方言和您使用的引号字符,您可能需要调整 col.value.split(" ")[-1].strip("`").rpartition('.')[- 1]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM