Pyodbc是否支持任何形式的命名参数? [英] Does pyodbc support any form of named parameters?
问题描述
我知道sqlite3有
I know sqlite3 has
data = {'test_col': 012345679}
sqlite3_conn.execute("""
UPDATE test_db SET test_col = :test_col
;""", data)
和mysql-connector-python具有
and mysql-connector-python has
data = {'test_col': 012345679}
mysql_conn.execute("""
UPDATE test_db SET test_col = %(test_col)s
;""", data)
但是pyodbc是否支持任何形式的命名参数?我喜欢能够将dict
传递给execute方法.这非常方便,并且对于我的某些查询(例如INSERT INTO ... ON DUPLICATE KEY UPDATE
),这是必需的.
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.
推荐答案
它不支持命名参数,但是以正确顺序传递的绑定参数非常简单:
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)
或
mssql_cur.execute("SELECT * FROM mytable WHERE colx = ? AND coly = ?", (x, y))
此处有更多详细信息和选项,例如传递executemany
参数:
More details and options here, such as passing executemany
parameters:
https://github.com/mkleehammer/pyodbc/wiki/Cursor
祝你好运!
这篇关于Pyodbc是否支持任何形式的命名参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!