Python sqlite3查询生成

2024-10-05 10:53:17 发布

您现在位置:Python中文网/ 问答频道 /正文

具有下列特征:

array = ['', 'kujawski=', "'", "select * from symbols where name = '='", ';drop table;', 'fakeone=']

我可以轻松生成以下查询:

//(query, array)
('select count(*) from symbols where  name in (?,?,?,?,?,?)', ('', 'kujawski=', "'", "select * from symbols where name = '='", ';drop table;', 'fakeone='))

要生成可以放入cursor.execute()函数中的查询,我目前正在通过以下代码执行:

"select count(*) from table where name in (%s)" % ",".join("?"*len(array)),array

函数返回所需的输出。然而,问题是何时我想使用AND筛选查询,例如:

select count(*) from table where name in (...) and column5 in (...)

我不知道如何在python中生成查询,哪个cursor.execute()函数将接受它,请帮助-谢谢


Tags: 函数nameinfromexecutecounttablewhere
1条回答
网友
1楼 · 发布于 2024-10-05 10:53:17

我不确定您的第二个带有连接的select语句想要实现什么。第一行是您应该传递给cur.execute()的内容。例如:

`cur.execute(
    'select count(*) from symbols where  name in (?,?,?,?,?,?)',
    ('', 'kujawski=', "'", "select * from symbols where name = '='", ';drop table symbols;', 'fakeone=')
)`

这也将防止SQL注入攻击,这正是您自己试图利用的

如果要执行drop语句等,那么应该使用cur.execute()自行处理,而不是像您正在尝试的那样,将其作为另一个命令中的嵌入命令

事实上,我完全不知道你想用这个来完成什么。这是您在sqlite完成后的声明(大致)

select
    count(*)
from
    symbols
where
    name in (
        '',
        'kujawski=',
        "'",
        "select * from symbols where name = '='",
        ';drop table symbols;',
        'fakeone=')

相关问题 更多 >

    热门问题