It makes sense that this works because this statement looks like this:
'some value goes here %s' % value
Actually returns a string. This is probably more logical for viewing it as follows:
result = ("%s limit 1" % sql) % table
There is nothing obviously wrong with this, but chain operators can lead to problems figuring out where the error came from.
So, for example, this works great:
>>> sql = 'a value of %s' >>> x = 'some string %s with stuff' >>> y = 'VALUE' >>> x % sql % y 'some string a value of VALUE with stuff'
But if there was a formatting error (I understand that this example is pathological, but it gets the point):
>>> sql = 'a value of %d' >>> x = 'some string %d with stuff' >>> y = 123 >>> x % sql % y Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: %d format: a number is required, not str
It is actually unclear which %d is causing your error. For this reason, I would split it and just use one % formatter per line, if possible, because then the trace will be able to tell you exactly which line and in which formatting the problem occurred.
For writing, by doing this with one formatting per line, you will also make life easier for others who need to read your code and try to figure out what is going on.
source share