Skip to content
Advertisement

String formatting — skip one format string

I have the following sql statement:

SELECT_CLAUSE += ", IF(%s IS NULL, -1, %s=%s) AS %s_score" % (
    field, field, '%s', field
)

Basically I want to string-format everything except for the second-to-last format string so it looks like:

field = 'name'
SELECT_CLAUSE += ', IF(name IS NULL, -1, name=%s) AS name_score'

What would be the best way to do this?

Advertisement

Answer

Double %% the single % to escape it:

SELECT_CLAUSE += ", IF(%s IS NULL, -1, %s=%%s) AS %s_score" % (
    field, field, field
)

Where is the StringBorg that we direly need? That would make this safe.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement