user8128927
user8128927

Reputation:

Python SELECT Query PYODBC

I have the following function

def retrieve_metadata(self, sample_name, workflow_execution_id):
    print("Starting to retrieve meta data from Workflow for Sample {} and WorkflowExection {}".format(
        sample_name, workflow_execution_id))
    sql_query = """SELECT s.SampleName, s.ClientSampleId, s.ClientSubjectId, p.ProjectName, c.ClientName, s.ProjectId, e.WorkflowExecutionId from Sample s join project p on s.ProjectId = p.ProjectId join Client C on p.ClientId = c.ClientId left join SampleExecution e on e.SampleId = s.SampleId where s.SampleName = %s and e.WorkflowExecutionId = %s"""
    package_data = (sample_name, workflow_execution_id)
    for row in self.sql_reader.cursor.execute(sql_query, package_data):
        temp_meta = MetaData(row.SampleName,
                             row.ClientSampleId,
                             row.ClientSubjectId,
                             row.ProjectName,
                             row.ClientName,
                             row.ProjectId,
                             row.WorkflowExecutionId,
                             )
        self.input_data.append(temp_meta)

I recently changed the code to accept the parameters, but the error I am getting is

  File "/var/task/sql_scrapper.py", line 21, in retrieve_metadata
    for row in self.sql_reader.cursor.execute(sql_query, package_data):
ProgrammingError: ('The SQL contains 0 parameter markers, but 2 parameters were supplied', 'HY000')

How can I resolve this with PYODBC?

Upvotes: 0

Views: 260

Answers (1)

user8128927
user8128927

Reputation:

    sql_query = """SELECT s.SampleName, s.ClientSampleId, s.ClientSubjectId, p.ProjectName, c.ClientName, s.ProjectId, e.WorkflowExecutionId from Sample s join project p on s.ProjectId = p.ProjectId join Client C on p.ClientId = c.ClientId left join SampleExecution e on e.SampleId = s.SampleId where s.SampleName = ? and e.WorkflowExecutionId = ?"""

changing from %s to ? resolved the issue.

Upvotes: 1

Related Questions