Python 3 sqlite 参数化 SQL 查询

B.T*_*ris 3 python sqlite python-3.x

我一直在尝试使用 Python 3 和 sqlite 模块进行参数化 SQL 查询,并且仅使用一个变量就成功了。但是,当使用两个变量时,出现IndexError: tuple index out of range错误。关于导致此错误的原因有什么建议吗?

sql = ("select exists(SELECT * from USERS where PASSWORD = '{0}' AND USERNAME = '{1}')")
args = (var1,var2)
cursor = database_connection.execute((sql).format(args))
Run Code Online (Sandbox Code Playgroud)

P-G*_*-Gn 11

永远不要在你的 sql 命令中填写原始条目,这是在调用 sql 注入攻击。

使用内置的填充功能。

sql = "select exists(SELECT * from USERS where PASSWORD = ? AND USERNAME = ?)"
args = (var1,var2)
cursor = database_connection.execute(sql, args)
Run Code Online (Sandbox Code Playgroud)