我想创建一个允许用户输入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()
这不会返回错误,但也不会返回预期的结果。它什么都不返回。
我用
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))