I am using pyscopg2 and have a method that returns a list based on a SQL query. The below works fine...
def checkAnalysisStartDate(self):
session = self.connection()
cursor = session.cursor()
ids = self.getAnalysisIds() # this is a list of integers
cursor.execute("select start_date from analysis_run_history where analysis_id in %s", [tuple(ids)])
final_result = [i[0] for i in cursor.fetchall()]
I want to pass the same list of integers but this time return a DataFrame from the results. When I try this though....
import pandas.io.sql as sqlio
def getAnalysisMetaStatsDF(self):
session = self.connection()
ids = self.getAnalysisIds() # this is a list of integers
data = sqlio.read_sql_query("Select * from analysis_stats where analysis_id in %s", [tuple(ids)], session)
print(data)
I get back...
AttributeError: 'list' object has no attribute 'cursor'
I think it's something with the way I am passing the parameters to read_sql_query() but I am not sure how to fix it
Whenever you call a function make sure that the call matches its signature. You can look up the signature by running
help(sqlio.read_sql_query), typingsqlio.read_sql_query?in IPython or in the documentation. You will find something like this:Notice that the second parameter is the database connection object, not the parameters of the query. In your case a call like the following is more appropriate: