我在 python 模块中有以下方法,它采用所有默认参数,并通过以下方式从这些参数生成 SQL 查询:
想知道,是否有更好(优雅和 Pythonic)的方式来生成上述 SQL 查询
def myquery(arg1=None, arg2=None, arg3=None):
query = "SELECT column FROM table WHERE"
params = []
if arg1 is not None:
query = query + " arg1 = " + arg1
params.append(arg1)
if arg2 is not None:
if params is not None:
query = query + "AND arg2 = " + arg2
else:
query = query + " arg 2 = " + arg2
params.append(arg2)
if arg3 is not None:
if params is not None:
query = query + "AND arg3 = " + arg3
else:
query = query + " arg 3 = " + arg3
params.append(arg3)
return query