Format Specifiers and String Concatenation

I am attempting to build up a fairly large SQL string and am having issues if Format Specifiers are on multiple lines of a concatenated string.

This works fine:

strSQL = "INSERT INTO tblTest (Col1, Col2, Col3) VALUES (%s, %s, %s)" % (1, 2, 3)

This fails:

strSQL = "INSERT INTO tblTest (Col1, Col2, Col3) " + \ "VALUES (%s, %s," + \ " %s)" % (1, 2, 3)

Maybe try using Python’s multi-line string instead:

sql = \
"""
INSERT INTO tblTest (Col1, Col2, Col3) 
VALUES (%s, %s, %s)
""" % (1, 2, 3)
		
print sql

or

sql = """
INSERT INTO tblTest (Col1, Col2, Col3) 
VALUES (%s, %s, %s)
""" % (1, 2, 3)
		
print sql

if you prefer not to have backslash’s littering your code.

Worth noting is that the string this builds will actually have newline characters in it, but for SQL queries I think that’s ok…