简体   繁体   English

在sqlite3中执行时,如何在元组多倍时间内使用值

[英]How do I use the values in the tuple miltiple times when doing an execute in sqlite3

I'm trying to use the execute function of sqlite3 to sanitize the string that I got. 我正在尝试使用sqlite3的execute函数来清理得到的字符串。 However, I can't do it in my case because I can't figure out how to use the values in the tuple more than one time. 但是,我无法做到这一点,因为我无法弄清楚如何多次使用元组中的值。

What I can do is this, 我能做的就是这个

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]))

However, I learnt that it is open for sqlinjection attacks since the input is being directly used in here. 但是,我了解到它可以进行直射攻击,因为此处直接使用了输入。 Is there a way I could still use tuples at the end of the execute function multiple times? 有没有一种方法可以在执行函数的结尾多次使用元组?

sqlite3 puts their sanitation front-and-center in the docs. sqlite3将卫生设施放在文档的首位。 Use the named placeholder style. 使用命名的占位符样式。

https://docs.python.org/3/library/sqlite3.html#sqlite3.Cursor.execute 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})

This answer to a previous post could help you visualize the fix as well: https://stackoverflow.com/a/12184247/10553976 上一篇文章的答案也可以帮助您直观地看到此修复程序: https : //stackoverflow.com/a/12184247/10553976

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM