在 sqlite3 中执行时如何多次使用元组中的值
How do I use the values in the tuple miltiple times when doing an execute in 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})
这个对之前 post 的回答也可以帮助您直观地了解修复:
我正在尝试使用 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})
这个对之前 post 的回答也可以帮助您直观地了解修复: