gpt4 book ai didi

parameterbinding - 参数绑定(bind)不适用于 SQLite PRAGMA table_info

转载 作者:行者123 更新时间:2023-12-04 17:56:39 25 4
gpt4 key购买 nike

我正在使用用于 Python 的 sqlite3。为什么表达式的参数绑定(bind)不起作用:

self.cursor.execute("PRAGMA table_info(?)", table_name)

正如预期的那样?对于任何其他 SELECT查询它按预期替换我的参数。我现在用
self.cursor.execute("PRAGMA table_info('%s')" % table_name)

但这对于 SQL 注入(inject)是不安全的。我该如何解决这个问题?

最佳答案

我想做同样的事情,但它看起来像 it's not possible to bind parameters to Sqlite PRAGMAs .

为了保持安全(以及我自己最终可能会做的事情),您可以做的是 query all the table names in the current Sqlite database在 SQL 中,像这样:

SELECT * FROM sqlite_master

或者,要获取表格并忽略 View ,请执行以下操作:
SELECT * FROM sqlite_master where type="table"

然后将这些表名存储在一个数组/列表/集合中。现在您在数据库中拥有所有可能表的列表,您可以简单地检查用户输入以查看它是否与数组中的表之一匹配。如果是,那么直接插入字符串是安全的,不会有SQL注入(inject)的机会。从本质上讲,它正在根据白名单进行清理。

在 Python 中,它看起来像这样:
if table_name in tables:
self.cursor.execute("PRAGMA table_info('%s')" % table_name)
else:
print("Bad table name: %s" % table_name)

关于parameterbinding - 参数绑定(bind)不适用于 SQLite PRAGMA table_info,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39985599/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com