Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/334.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 Server_Openquery - Fatal编程技术网

如何在python中的sql参数化查询的单引号中放置参数标记

如何在python中的sql参数化查询的单引号中放置参数标记,python,sql-server,openquery,Python,Sql Server,Openquery,我想用python编写一个sql openquery。我编写的常规sql查询如下所示: sql_query = select name from emp where id= %s 我执行的时候就像: cursor.execute(sql\u查询,(id\u值,) 而且效果很好 但是,现在我有一个openquery,它类似于: sql_query = select * from openquery([LS], 'select name from \"DB\"."view" where \"id

我想用python编写一个sql openquery。我编写的常规sql查询如下所示:

sql_query = select name from emp where id= %s 
我执行的时候就像:
cursor.execute(sql\u查询,(id\u值,)

而且效果很好

但是,现在我有一个openquery,它类似于:

sql_query = select * from openquery([LS], 'select name from \"DB\"."view" where \"id\" Like %s') 
如果我使用:
cursor.execute(sql\u查询,(id\u值,)

我会得到一个错误,说:

SQL包含0个参数标记,但提供了1个参数

我知道会出现此错误,因为%s是在单个带引号的查询中指定的。但是我不能删除这个单引号,因为如果SQL server本身没有它,查询将无法工作

我已尝试使用以下工具运行查询:

cursor.execute(sql_query % (id_value))
它是有效的。但是我不想使用这种格式,因为这种格式容易被SQL注入

那么,如何用python编写安全的参数化openquery。

请在“id\u值”之后的参数块中删除额外的逗号(“,”):


你能解释一下答案吗?这没关系,我得到了一个错误:SQL包含0个参数标记,但提供了1个参数。这意味着参数已读。在编辑此答案之前,您能否解释您的答案
sql_query = select * from openquery([LS], 'select name from \"DB\"."view" where \"id\" Like %s')    

cursor.execute(sql_query, (id_value))