Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python中SQL语句中的变量_Python_Sql_Postgresql_Psycopg2 - Fatal编程技术网

Python中SQL语句中的变量

Python中SQL语句中的变量,python,sql,postgresql,psycopg2,Python,Sql,Postgresql,Psycopg2,我有一个包含ID和字符串变量的整数列表。如何在SQL语句中使用这些变量?如果我使用这个: list_of_ids = [1,2,3] s_date = '2015-01-01' cursor.execute(""" SELECT * FROM foo WHERE id IN (%s) AND start_date=%s """, (list_of_ids,s_date)) _id的列表将用引号括起来,不应该用引号括起来 这个问题与此相关,但仅限于声明部分 我正在使用psyc

我有一个包含ID和字符串变量的整数列表。如何在SQL语句中使用这些变量?如果我使用这个:

list_of_ids = [1,2,3]
s_date = '2015-01-01'

cursor.execute("""
   SELECT * FROM foo WHERE id IN (%s)
   AND start_date=%s
   """, (list_of_ids,s_date))
_id的列表将用引号括起来,不应该用引号括起来

这个问题与此相关,但仅限于声明部分

我正在使用psycopg2连接——以防有帮助。

构建为序列(以下示例中的列表)。您需要相应地调整sql部分

in_part = ','.join('%s' for _ in list_of_ids)
sql = "SELECT * FROM foo WHERE id IN (%s) AND start_date=%%s" % (in_part,)
params = list_of_ids + [s_date]  # [1, 2, 3, '2015-01-01']
cursor.execute(sql, params)
构建为序列(以下示例中的列表)。您需要相应地调整sql部分

in_part = ','.join('%s' for _ in list_of_ids)
sql = "SELECT * FROM foo WHERE id IN (%s) AND start_date=%%s" % (in_part,)
params = list_of_ids + [s_date]  # [1, 2, 3, '2015-01-01']
cursor.execute(sql, params)

要使用语法中的
,请将列表转换为元组:

list_of_ids = [1,2,3]
s_date = '2015-01-01'

query = """
    select *
    from foo
    where id in %s and start_date = %s
"""
print cursor.mogrify(query, (tuple(list_of_ids), s_date))
#cursor.execute(query, (tuple(list_of_ids), s_date))
输出:

select *
from foo
where id in (1, 2, 3) and start_date = '2015-01-01'
select *
from foo
where id = any (ARRAY[1, 2, 3]) and start_date = '2015-01-01'
要通过列表而不强制转换,请使用
=any
语法:

query = """
    select *
    from foo
    where id = any (%s) and start_date = %s
"""
print cursor.mogrify(query, (list_of_ids, s_date))
#cursor.execute(query, (list_of_ids, s_date))
输出:

select *
from foo
where id in (1, 2, 3) and start_date = '2015-01-01'
select *
from foo
where id = any (ARRAY[1, 2, 3]) and start_date = '2015-01-01'

要使用
语法中的
,请将列表转换为元组:

list_of_ids = [1,2,3]
s_date = '2015-01-01'

query = """
    select *
    from foo
    where id in %s and start_date = %s
"""
print cursor.mogrify(query, (tuple(list_of_ids), s_date))
#cursor.execute(query, (tuple(list_of_ids), s_date))
输出:

select *
from foo
where id in (1, 2, 3) and start_date = '2015-01-01'
select *
from foo
where id = any (ARRAY[1, 2, 3]) and start_date = '2015-01-01'
要通过列表而不强制转换,请使用
=any
语法:

query = """
    select *
    from foo
    where id = any (%s) and start_date = %s
"""
print cursor.mogrify(query, (list_of_ids, s_date))
#cursor.execute(query, (list_of_ids, s_date))
输出:

select *
from foo
where id in (1, 2, 3) and start_date = '2015-01-01'
select *
from foo
where id = any (ARRAY[1, 2, 3]) and start_date = '2015-01-01'
请参阅上的顶部答案,其中详细说明了正确的操作方法。请参阅上的顶部答案,其中详细说明了正确的操作方法。