我试图找到格式化sql查询字符串的最佳方法.当我调试的时候
Option 1
def myquery():
sql = "select field1, field2, field3, field4 from table where condition1=1 and condition2=2"
con = mymodule.get_connection()
...
- 这适用于打印sql字符串.
- 如果绳子很长且不符合标准宽度,这不是一个好的解决方案
Option 2
def query():
sql = """
select field1, field2, field3, field4
from table
where condition1=1
and condition2=2"""
con = mymodule.get_connection()
...
-
这里的代码很清楚,但是当你打印sql查询字符串时,你会得到这些恼人的空格.
u'\n Select 字段1、字段2、字段3、字段4\n_____from table\n____where condition1=1 \n_____和条件2=2'
Note: I have replaced white spaces with underscore 100, because they are trimmed by the editor
Option 3
def query():
sql = """select field1, field2, field3, field4
from table
where condition1=1
and condition2=2"""
con = mymodule.get_connection()
...
- 我不喜欢这个选项,因为它 destruct 了列表良好的代码的清晰性.
Option 4
def query():
sql = "select field1, field2, field3, field4 " \
"from table " \
"where condition1=1 " \
"and condition2=2 "
con = mymodule.get_connection()
...
- 我不喜欢这个选项,因为每行都有额外的输入
对我来说,最好的解决方案是Option 2,但我不喜欢打印sql字符串时额外的空格.
你知道还有其他 Select 吗?