简体   繁体   English

如何使用python从SQL查询中提取列名和条件

[英]How to extract column names along with conditions from a SQL query using python

How to extract column names along with their conditions applied from a SQL query string using Python, so that I can map the result to other table and get the required data?如何使用 Python 从 SQL 查询字符串中提取列名及其条件,以便我可以将结果映射到其他表并获取所需的数据?

query = "SELECT COUNT(*) as cnt FROM dbo.main_table WHERE (VKRKA10001920015 = 1 AND age IN (15, 16, 17, 18, 19) AND income IN (101, 102, 301, 302, 305))"

Required_Output = {
  'VKRKA10001920015': 1,
  'age' : (15, 16, 17, 18, 19),
  'income' : (101, 102, 301, 302, 305),
}

As of now I am able to extract only column names using sql-metadata python package.到目前为止,我只能使用 sql-metadata python 包提取列名。 But I need the conditions as well to perform other operations但我也需要条件来执行其他操作

from sql_metadata.compat import get_query_columns
print(get_query_columns(query))

['VKRKA10001920015', 'age', 'householdincome']

To identify the tokens ("IN", "=", "LIKE") I have used the get_tokens() which recursively get the tokens added to the extracted_key() dictionary.为了识别标记(“IN”、“=”、“LIKE”),我使用了 get_tokens(),它递归地将标记添加到 extract_key() 字典中。 Here in the extracted_keys dict the key address stored instead of the actual key Since the functions is going recursively.由于函数是递归的,因此在提取的密钥字典中存储的密钥地址而不是实际密钥。 So I have used create_dict() another function to create a proper dictionary out of extracted_keys dictionary.所以我使用 create_dict() 另一个函数从提取的键字典中创建一个正确的字典。 For now we are able to extract columns along with their conditions from the SQL WHERE conditon for following operators ie, "IN" operator, "=" operator, "Like" operator.现在,我们能够从 SQL WHERE 条件中提取列及其条件以用于以下运算符,即“IN”运算符、“=”运算符、“Like”运算符。

To install sqlparse module安装 sqlparse 模块

pip install sqlparse

import sqlparse

def get_tokens(where):
  identifier = None
  extracted_keys = {}
  
  for i in where.tokens:
    try:
      name = i.get_real_name()
      
      if name and isinstance(i, sqlparse.sql.Identifier):
        name = i.get_real_name()
        identifier = i
        extracted_keys[identifier] = i.value
      
      elif identifier and isinstance(i, sqlparse.sql.Parenthesis):
        extracted_keys[identifier] = i.value
        
      elif i and "in" not in i.value.lower() and "or" not in i.value.lower() and isinstance(i, sqlparse.sql.Comparison):
        if "=" in i.value:
            equal_index = i.value.index("=")
            if i.value[equal_index -1] not in ["!", ">", "<"]:
              key,value = i.value.split("=")[0], i.value.split("=")[1]
              extracted_keys[key] = value
            
        if "!=" in i.value:
            key,value = i.value.split("!=")[0], i.value.split("!=")[1]
            extracted_keys[key] = value
            
        elif "like" in i.value.lower():
            key,value = i.value.lower().split("like")[0].upper(), i.value.lower().split("like")[1]
            extracted_keys[key] = value
        
      else:
        extracted_keys.update(get_tokens(i))
    
    except Exception as error:
      pass
    
  return extracted_keys

def create_dict(extracted_keys):
  cols = {}
  for key, value in extracted_keys.items():
    try:
      if key.get_real_name():
        cols[key.get_real_name()] = value
    except Exception as e:
      cols[key] = value
  return cols

For example:例如:

query = "SELECT COUNT(*) as cnt FROM dbo.main_table WHERE (VKRKA10001920015 = 1 AND age IN (15, 16, 17, 18, 19) AND income IN (101, 102, 301, 302, 305))"

parsed = sqlparse.parse(query)
where = parsed[0][-1]
extracted_keys = get_tokens(where)

extracted_keys = create_dict(extracted_keys)

Output输出

{
  'VKRKA10001920015': 1,
  'age' : (15, 16, 17, 18, 19),
  'income' : (101, 102, 301, 302, 305),
}

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

相关问题 如何使用 Python 从 SQL 查询中提取列名 - How to extract column names from SQL query using Python 使用 sqlparse 从 Python 中的 SQL 查询中提取所有列名 - extract all column names from an SQL query in Python using sqlparse 如何从SQL查询中提取列名 - How to extract the column names from a sql query 如何从sql查询中提取表名和列名? - How to extract table names and column names from sql query? 如何使用循环打印 sqlite3 中的表以及 python 中的列名以及如何准确获取列名? - How to print a table in sqlite3 along with column names in python using a loop and how exactly to get the column names? 如何使用类似运算符从火花 sql 中的 python 列表中的多个条件构造查询? - How to construct query using like operator for multiple conditions from a python list in spark sql? 在SQL查询中使用Python列表作为列名 - Use Python list in SQL query for column names 如何使用 Spacy 从 Python 中的数据框中提取人名 - How to extract Person Names from a data frame in Python using Spacy 如何使用 Python 和 BeautifulSoup 从 cricinfo 中提取玩家姓名 - How to extract player names using Python with BeautifulSoup from cricinfo 如何使用Python从串联字符串中提取名称? - How can I extract names from a concatenated string using Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM