I am trying to find the best way to format sql query string. When I debug my application I would like to register in order to write all sql query strings, and it is important that the string is correctly formed.
Option 1
def myquery(): sql = "select field1, field2, field3, field4 from table where condition1=1 and condition2=2" con = mymodule.get_connection() ...
- This is useful for printing sql strings.
- This is not a good solution if the string is long and does not match the standard width of 80 characters.
Option 2
def query(): sql = """ select field1, field2, field3, field4 from table where condition1=1 and condition2=2""" con = mymodule.get_connection() ...
Note. I replaced the white spaces with the underscore _ because they were 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() ...
- I do not like this parameter because it violates the clarity of the code with the table with the table.
Option 4
def query(): sql = "select field1, field2, field3, field4 " \ "from table " \ "where condition1=1 " \ "and condition2=2 " con = mymodule.get_connection() ...
- I do not like this parameter, because there is all additional input on each line and it is also difficult to edit the query.
For me, Option 2 would be the best solution, but I don't like the extra spaces when printing an sql string.
Do you know any other options?
python sql string-formatting
ssoler Mar 09 '11 at 9:20 2011-03-09 09:20
source share