3

I have read dozens of similar posts and tried everything but I still get an error message when trying to pass a parameter to a simple query using pyodbc. Apologies if there is an answer to this elsewhere but I cannot find it

I have a very simple table:

select * from Test

yields

a
b
c

This works fine:

import pyodbc
import pandas
connection = pyodbc.connect('DSN=HyperCube SYSTEST',autocommit=True)
result = pandas.read_sql("""select * from Test where value = 'a'""",connection,params=None)
print(result)

result:

  value
0     a

However if I try to do the where clause with a parameter it fails

result = pandas.read_sql("""select * from Test where value = ?""",connection,params='a')

yields

Error: ('01S02', '[01S02] Unknown column/parameter value (9001) (SQLPrepare)')

I also tried this

cursor = connection.cursor()
cursor.execute("""select * from Test where value = ?""",['a'])
pyodbcResults = cursor.fetchall()

and still received the same error

Does anyone know what is going on? Could it be an issue with the database I am querying?

PS. I looked at the following post and the syntax there in the first part of answer 9 where dates are passed by strings looks identical to what I am doing

pyodbc the sql contains 0 parameter markers but 1 parameters were supplied' 'hy000'

Thanks

CT00
  • 77
  • 1
  • 2
  • 9
  • Possible duplicate of [pyodbc the sql contains 0 parameter markers but 1 parameters were supplied' 'hy000'](https://stackoverflow.com/questions/43491381/pyodbc-the-sql-contains-0-parameter-markers-but-1-parameters-were-supplied-hy0) – Adam Silenko Apr 18 '18 at 21:30
  • Maybe a compatibility issue with your ODBC driver? `result = pd.read_sql(sql, cnxn, params=('a',))` works for me with pyodbc 4.0.23 and ODBC Driver 11 for SQL Server using `?` as the parameter placeholder (`... WHERE value = ?`). Can you test using some other database platform? – Gord Thompson Apr 18 '18 at 23:47

1 Answers1

3

pandas.read_sql(sql, con, index_col=None, coerce_float=True, params=None, parse_dates=None, columns=None, chunksize=None)[https://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_sql.html]

params : list, tuple or dict, optional, default: None

example:

cursor.execute("select * from Test where value = %s",['a'])

or Named arguments example:

result = pandas.read_sql(('select * from Test where value = %(par)s'),
               db,params={"par":'p'})

in pyodbc write parms directly after sql parameter:

cursor.execute(sql, *parameters)

for example:

onepar = 'a'
cursor.execute("select * from Test where value = ?", onepar)

cursor.execute("select a from tbl where b=? and c=?", x, y)
Adam Silenko
  • 3,025
  • 1
  • 14
  • 30
  • Although all of the pyodbc documentation states to use ?, I tried %s also and got a different error message that seems to indicate that %s is not a valid marker: ProgrammingError: ('The SQL contains 0 parameter markers, but 1 parameters were supplied', 'HY000') – CT00 Apr 18 '18 at 21:17
  • Thanks but unfortunately I also tried the named argument route but also received the same error: ProgrammingError: ('The SQL contains 0 parameter markers, but 1 parameters were supplied', 'HY000') – CT00 Apr 18 '18 at 21:53
  • read about [pyodbc - Cursor](https://code.google.com/archive/p/pyodbc/wikis/Cursor.wiki) – Adam Silenko Apr 18 '18 at 22:00
  • Tried your exact code above (first two lines) and get the original error - this just seems bizarre - I have tried every possible example and I can't figure it out : Error: ('01S02', '[01S02] Unknown column/parameter value (9001) (SQLPrepare)') – CT00 Apr 18 '18 at 22:17
  • `con = pyodbc.connect('DSN=HyperCube SYSTEST',autocommit=True) cur = cnxn.cursor() cur.execute("""select * from Test where value = ?""", 'a') rows = cursor.fetchall()` – Adam Silenko Apr 18 '18 at 22:57