将数组参数传递给 SQL 命令

cla*_*lay 1 python postgresql python-2.7

在 Python 2.7 中,我可以这样做,将参数传递给 sql 命令,如下所示:

cursor.execute("select * from my_table where id = %s", [2])
Run Code Online (Sandbox Code Playgroud)

我无法像这样获得等效的数组:

cursor.execute("select * from my_table where id in %s", [[10,2]])
Run Code Online (Sandbox Code Playgroud)

显然,我只能进行字符串格式化,但如果可能的话,我想做一个适当的参数。如果重要的话,我正在使用 postgresql 数据库。

f43*_*d65 5

cursor.execute("select * from my_table where id = ANY(%s);", [[10, 20]])
Run Code Online (Sandbox Code Playgroud)

。要使用IN请参阅下面的部分

cursor.execute(cursor.mogrify("select * from my_table where id in %s",
                              [tuple([10, 20])]))
Run Code Online (Sandbox Code Playgroud)