【发布时间】:2020-07-09 12:06:54
【问题描述】:
函数处理sql请求。它接收参数,其中一个可以是“null”或值(uuid), 我需要将此值添加到位置。 但是有一个问题,因为用null “其中 param 为 null”,但值为“where param = value”
例子
sql = '''select name from table where param = %s '''
def run_sql(param=None):
a = db_api(sql, param)
【问题讨论】:
-
你不能用
if/else吗? -
你能告诉我我的例子吗?
-
难道你不知道如何使用
if/else并针对不同的数据类型运行两个不同的查询吗? -
if param == "null" : sql = '''select name from table where param is %s ''' else: sql = '''select name from table where param = %s ''' -
我不考虑这种方法,因为工作的 sql 查询比示例中的要大得多。我想避免代码重复。
标签: python sql postgresql flask psycopg2