5

I am learning python, and I am trying to pass a parameter as part of the WHERE clause. I am using pandas and pyodbc. Here is what I've tried so far. I first get the data for column c from a pandas dataframe, and convert it to a list called df_col, which has about 100 numeric values

df_col=df['data_for_colc'].tolist()

then, I execute the SQL statement:

execu = mycursor.execute(
"""
Select 
 columnA
 ,columnb
 ,columnc
where
 columnc in (?)
""",df_col)

rows = mycursor.fetchall()
print(rows)

I am able to connecto and download data from SQL server, but I am not able to pass parameters. I just need to be able to download those 100 rows based on the list I created with 100 values, but I get an error: ('42000', "[42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near ','. (102) (SQLExecDirectW)")

any help would be appreciated. Thanks

1
  • Your SELECT statement has no FROM clause. Commented Jan 25, 2018 at 0:14

2 Answers 2

8

The syntax error is because you left out the FROM clause in your query.

Once you fix that, you need to have as many ? in the IN() list as there are elements in df_col.

placeholders = ", ".join(["?"] * len(df_col))
sql = """
    SELECT columnA, columnB, columnC
    FROM yourTable
    WHERE columnc IN (""" + placeholders + ")"
execu = mycursor.execute(sql, df_col)
rows = mycursor.fetchall()
print(rows)
Sign up to request clarification or add additional context in comments.

5 Comments

True, but the wrong number of parameter placeholders does not explain the error in syntax. The problem with the query stated in the question is more fundamental than that.
yeah, that's because he forgot FROM tablename
@roganjosh The other answer uses .format(), so I'll leave mine with string concatenation. But I've moved the joining out to another variable.
thank you. It was not working at the beginning, but I figured out it was missing some "" at the end. Thank you very much.
@barmar yes, I forgot the FROM in the SQL statement. my bad.
3

you have to generate all those question marks...

execu = mycursor.execute(
"""
Select 
 columnA
 ,columnb
 ,columnc
where
 columnc in ({})
""".format(','.join("?"*len(df_col))), df_col)

rows = mycursor.fetchall()
print(rows)

2 Comments

@Barmar why would I want a list... I can use','.join() with a string. It works fine as it is
Ah, right, I forgot that join will iterate over the characters of the string. Too much PHP/JS...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.