简体   繁体   English

Python sqlite3 查询生成

[英]Python sqlite3 query generation

Having following:具有以下:

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

I can easly generate following query:我可以轻松生成以下查询:

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

to generate query which I can put into cursor.execute() function and I'm currently doing it by following code:生成可以放入 cursor.execute() 函数的查询,我目前正在通过以下代码执行此操作:

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

And cursor.execute() function return desired output.并且 cursor.execute() 函数返回所需的输出。 However problem is when I would like to filter query with AND, for example:但是问题是当我想用 AND 过滤查询时,例如:

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

I have no idea how to generate query in python which cursor.execute() function will accept it, please help - thank you!我不知道如何在 python 中生成哪个 cursor.execute() 函数会接受它的查询,请帮忙 - 谢谢!

I'm not sure what you're trying to accomplish with your second select statement with the joins.我不确定你想用连接的第二个select语句来完成什么。 The first line is what you should pass to cur.execute() .第一行是您应该传递给cur.execute() For example:例如:

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

This will also prevent SQL injection attacks, which is what you yourself are trying to take advantage of.这也将防止 SQL 注入攻击,这是您自己试图利用的。

If you want to execute drop statements and the like, then you should be using cur.execute() to handle that on its own, not as an embedded command within another command like you're trying.如果你想执行drop语句等,那么你应该使用cur.execute()来处理它自己,而不是像你正在尝试的那样作为另一个命令中的嵌入命令。

In fact, I have no idea what you're trying to accomplish AT ALL with this.事实上,我完全不知道你想用这个来完成什么。 This is your statement (roughly) after sqlite gets done with it.这是您在 sqlite 完成后的声明(大致)。

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

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

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