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,如何才能使其生效。感觉像是用一个有条件的语句来做,所以不太可能在熊猫身上使用