I know sqlite3 has
data = {'test_col': 012345679} sqlite3_conn.execute(""" UPDATE test_db SET test_col = :test_col ;""", data)
and mysql-connector-python has
data = {'test_col': 012345679} mysql_conn.execute(""" UPDATE test_db SET test_col = %(test_col)s ;""", data)
but does pyodbc support any form of named parameters? I like being able to just pass a dict
to the execute method. It is very convenient, and with some of my queries, such as INSERT INTO ... ON DUPLICATE KEY UPDATE
, it is needed.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
It doesn’t support named parameters, but bound parameters passed in the correct order are fairly straightforward:
x = "This" y = 345 mssql_cur.execute("SELECT * FROM mytable WHERE colx = ? AND coly = ?", x, y)
or
mssql_cur.execute("SELECT * FROM mytable WHERE colx = ? AND coly = ?", (x, y))
More details and options here, such as passing executemany
parameters:
https://github.com/mkleehammer/pyodbc/wiki/Cursor
Good luck!
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0