提问人:Griffith 提问时间:6/4/2021 更新时间:6/4/2021 访问量:68
设置 SQL 查询的格式以接受带引号的值列表 [duplicate]
Format SQL query to accept list of quoted values [duplicate]
问:
我正在尝试使用 python3 sqlite3 API 从表中删除帐户列表。
查询应如下所示:
DELETE FROM table WHERE email in ("email1", "email2", "email3")
delete_list = [f'"{email}"' for email in emails]
query = f"DELETE FROM {table} WHERE email IN (" + ", ".join(delete_list) + ")"
try:
self.cursor.execute(query)
self.connection.commit()
return True
except sqlite3.OperationalError:
self.connection.rollback()
return False
此代码有效,但显然容易受到 SQL 注入的影响。有没有更安全的方法来使用 sqlite3 构建我的 SQL 查询?
答:
0赞
Griffith
6/4/2021
#1
这很完美。
query = f"DELETE FROM {table} WHERE email IN (" + ", ".join("?" * len(emails)) + ")"
try:
self.cursor.execute(query, emails)
self.connection.commit()
...
评论