我想在服务器中运行 sql 查询。而且查询太长,所以我无法弄清楚应该如何分解它:
查询看起来像这样:
return("usr/<path> -e 'select val1 from table_name where val2 = (select val3 f rom another_table where val = 'abc')'")
此外,我无法发布确切的查询,因为它与我的工作相关。所以现在的问题是,如果我运行一个位于 return 语句的单行中的选择查询,那么该值将成功返回。但在这个长查询的情况下,当它被 psuhed 到下一行时,我会收到语法错误。另外,代码必须与 Python 版本 2 和 3 兼容。有人可以帮忙吗?
您需要使用三引号:
类似:
return("""usr/<path> -e 'select val1 from table_name
where val2 = (select val3 f rom another_table where val = 'abc')'""")
三重引号字符串保留换行符。