[issue29668] f-strings don't change the values as expected.

Serhiy Storchaka report at bugs.python.org
Mon Feb 27 12:58:47 EST 2017


Serhiy Storchaka added the comment:

The expression is a concatenation of f-string expression f"SELECT COUNT(*) " and three string literals. If you want to substitute the "a" value, convert string literals into f-string expressions:

query = (f"SELECT COUNT(*) "
         f"FROM `{a}` entry "
         f"WHERE entry.type == 'device' "
         f"AND entry.instance == {a}")

or

query = ("SELECT COUNT(*) "
         f"FROM `{a}` entry "
         "WHERE entry.type == 'device' "
         f"AND entry.instance == {a}")

But be aware that using f-string expressions for formatting SQL queries is not safe in general case. Instead, use the DB-API’s parameter substitution.

----------
resolution:  -> not a bug
stage:  -> resolved
status: open -> closed

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue29668>
_______________________________________


More information about the Python-bugs-list mailing list