Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/354.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 pyodbc游标在有参数或无参数的情况下执行_Python_Pyodbc - Fatal编程技术网

Python pyodbc游标在有参数或无参数的情况下执行

Python pyodbc游标在有参数或无参数的情况下执行,python,pyodbc,Python,Pyodbc,如果没有提供参数,我如何执行查询而不这样做呢 def query_fetch(self, sql_query, params=None, fetchall=True, *args): with self.cursor.execute(sql_query) if params is None else self.cursor.execute(sql_query, params): row = self.cursor.fetchall() if fetchall else s

如果没有提供参数,我如何执行查询而不这样做呢

def query_fetch(self, sql_query, params=None, fetchall=True, *args):
    with self.cursor.execute(sql_query) if params is None else self.cursor.execute(sql_query, params):
        row = self.cursor.fetchall() if fetchall else self.cursor.fetchone()
        r = [d for d in row]
    return r

你已经在做的事情似乎完全合理。如果SQL命令文本不包含参数占位符,则必须仅使用单个字符串参数调用
.execute
方法。如果SQL命令文本包含参数占位符,则必须在初始字符串(命令文本)参数之后使用相同数量的参数值调用
。执行
方法。

您说的“不这样做”是什么意思?它与if-else条件有关吗?是的,因为我不确定如果execute-ignore参数为none,如何才能使其生效。感觉像是用一个有条件的语句来做,所以不太可能在熊猫身上使用