Python 在sqlite3中执行时,如何使用元组中的值
我试图使用sqlite3的execute函数来清理我得到的字符串。但是,在我的例子中,我不能这样做,因为我无法多次理解如何使用元组中的值 我能做的就是Python 在sqlite3中执行时,如何使用元组中的值,python,sqlite,sql-injection,Python,Sqlite,Sql Injection,我试图使用sqlite3的execute函数来清理我得到的字符串。但是,在我的例子中,我不能这样做,因为我无法多次理解如何使用元组中的值 我能做的就是 cursor.execute(""" select * from rides r where r.cno = c.cno and (r.src like '%{0}%'
cursor.execute("""
select *
from rides r
where r.cno = c.cno
and (r.src like '%{0}%'
or r.dst like '%{0}%'
or e.lcode like '%{0}%'
and (r.src like '%{1}%'
or l3.address like '%{1}%')
and (r.src like '%{2}%'
or l1.address like '%{2}%')
;
""".format(keywords[0], keywords[1], keywords[2]))
然而,我了解到它对sqlinjection攻击是开放的,因为这里直接使用输入。有没有办法在execute函数的末尾多次使用元组?sqlite3将它们放在文档的前面和中心。使用命名占位符样式 对上一篇文章的回答也可以帮助您可视化修复:
哇,谢谢你。作为解决方案,我所做的是一次又一次地重复元组中的变量以匹配问号。不过你的方式要好得多。谢谢你的回答,查尔斯·兰道。不客气!如果回复解决了您的问题,请将其标记为答案。
# This is the qmark style:
cur.execute("insert into people values (?, ?)", (who, age))
# And this is the named style:
cur.execute("select * from people where name_last=:who and age=:age", {"who": who, "age": age})