I have this query working through ODBC to a 4D database. It returns records as expected.
with pyodbc.connect('dsn=datasource') as db4dcon:
print(db4dcon)
db4dcur = db4dcon.cursor()
print(db4dcur)
db4dcur.execute("SELECT * FROM Sampling WHERE Id='2017-043';")
print(db4dcur.rowcount)
<pyodbc.Connection object at 0x029CC068>
<pyodbc.Cursor object at 0x0292B058>
3
I would like to change it to a parameterized version, but I could not succeed. I have read the doc, and none of those example work for me.
Neither:
db4dcur.execute("SELECT * FROM Sampling WHERE Id=?;", '2017-043')
Nor:
db4dcur.execute("SELECT * FROM Sampling WHERE Id=?;", ('2017-043',))
Nor:
db4dcur.execute("SELECT * FROM Sampling WHERE Id=?;", ['2017-043']))
Work. They all lead to the following error:
<pyodbc.Connection object at 0x02C7C068>
<pyodbc.Cursor object at 0x028DB058>
Traceback (most recent call last):
File "test.py", line 251, in <module>
main(sys.argv)
File "test.py", line 221, in main
db4dcur.execute("SELECT * FROM Sampling WHERE Id=?;", '2017-043')
pyodbc.Error: ('08004', '[08004] Server rejected the connection:\nFailed to execute statement.\r (1110) (SQLExecDirectW)')
I have checked my ODBC connection, it seems server rejects a malformed query when replacing wildcard, but I cannot check what it is sent because of ODBC mechanism. Does anyone have an idea about what is going wrong with this parameterized query?