创建一个函数将SQL查询转换为Pandas df

时间:2018-01-08 15:50:31

标签: python sql python-3.x pandas

我想创建一个允许用户输入SQL查询并将其转换为Pandas df的函数。到目前为止,我已尝试过以下内容:

def dataset():
    raw_sql_query = input("Enter your SQL query: ")
    sql_query = """" " + raw_sql_query + " """" 
sql3 =
sql_query
df = pd.io.sql.read_sql(sql3, cnxn)
df.head()

产生错误:

  File "<ipython-input-18-6b10c2bc776f>", line 4
    sql_query = """" " + raw_sql_query + " """"
                                                ^
SyntaxError: EOL while scanning string literal

我也尝试过上面代码的几个相似版本,包括:

def dataset():
    raw_sql_query = input("Enter your SQL query: ")
    sql_query = """"" + raw_sql_query + """"" 
sql3 =
sql_query 
df = pd.io.sql.read_sql(sql3, cnxn)
df.head()

导致以下错误:

  File "<ipython-input-23-e501c9746878>", line 5
    sql3 =
          ^
SyntaxError: invalid syntax

这样的功能可能吗?如果是这样,我将如何为此操作创建工作函数? 我读过的关于函数的所有文档仅包含打印“Hello World”或基本加法/减法/等内容的示例 - 因此不太有用。

编辑: 像这样使用pandas.read_sql_query

def dataset():
    """This functions allows you to input a SQL query and it will be transformed into a Pandas dataframe"""
    raw_sql_query = input("Enter your SQL query: ")
    sql_query = """"" + raw_sql_query + """"" 
sql3 = sql_query 
df = pd.io.sql.read_sql(sql3, cnxn)
df.head()

这不会返回错误,但也不会返回预期的结果。它什么都不返回。

2 个答案:

答案 0 :(得分:0)

我用

import pandas as pd
from sqlalchemy import create_engine
from sqlalchemy.sql import text as SQLtext
from sqlalchemy.sql import bindparam

engine = create_engine("postgres+psycopg2://"+settings.DBconn)
def readQuery(query, **params):
    query = SQLtext(query)
    for key, value in params.items():
        query = query.bindparams(bindparam(key, value))
    return(pd.read_sql(query, engine))

答案 1 :(得分:0)

我喜欢将sqlalchemypandas.read_sql结合使用的灵活性。这是我使用的代码:

import sqlalchemy as sa

def bindQuery(query, **params):
    for key, value in params.items():
        key =  f":{key}"
        if isinstance(value, str):
            value = f"'{value}'"
        query = query.replace(key, str(value))
    query = query.replace("\n", " ").replace("\t", " ")
    return query


def readQuery(query, engine, **params):
    query = bindQuery(query, **params)
    return pd.read_sql(query, engine)

因此,当我必须运行以下QUERY


QUERY = """
SELECT count(*) 
FROM table
where id in :ids
"""

ids = (1, 2, 3)

df = readQuery(query=QUERY,
               engine=my_engine,
               ids=ids)