python - 在 sqlite3 中执行时如何使用元组中的值
问题描述
我正在尝试使用 sqlite3 的执行功能来清理我得到的字符串。但是,在我的情况下我不能这样做,因为我不知道如何多次使用元组中的值。
我能做的就是这个
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 攻击是开放的,因为这里直接使用了输入。有没有办法我仍然可以在执行函数的末尾多次使用元组?
解决方案
sqlite3 将他们的卫生设施放在文档的中心位置。使用命名的占位符样式。
https://docs.python.org/3/library/sqlite3.html#sqlite3.Cursor.execute
# 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})
这个对上一篇文章的回答也可以帮助您可视化修复: https ://stackoverflow.com/a/12184247/10553976
推荐阅读
- wireguard - Wireguard - 如何使用 Wireguard VPN 拆分隧道限制对没有静态 IP 的应用程序的 VPN 访问?
- css - 背景图像:url() 没有从 css 文件加载图像
- sas - 如何使用proc sql从sas中的以下数据中获取第一行
- javascript - 如何格式化具有空行的 excel 文件以具有嵌套数组并匹配我需要呈现的 JSON 对象?
- c# - 尽可能快地从 WCF 使用 Stream
- javascript - 调用另一个函数,其中函数名称是 NodeJS 中的字符串
- c# - 在 ForEach 循环中链接顺序异步任务:这是一个好方法吗?
- javascript - 将 & 变成 & 使用 Javascript
- swift - 在 SceneDelegate SwiftUI 上设置 managedObjectContext
- python - Out-print of a function-call