This one works (one line only):
c.execute(f"SELECT Firm, Platform, `Sale in million` FROM database WHERE Platform IN ({','.join('?' for _ in list_table)}) ORDER BY Firm", (list_table))
But these ones don't (2 lines):
c.execute(f"SELECT Firm, Platform, `Sale in million` FROM database
WHERE Platform IN ({','.join('?' for _ in list_table)}) ORDER BY Firm", (list_table))
c.execute(f"SELECT Firm, Platform, `Sale in million` FROM database"
f"WHERE Platform IN ({','.join('?' for _ in list_table)}) ORDER BY Firm", (list_table))"
c.execute("SELECT Firm, Platform, `Sale in million` FROM database"
f"WHERE Platform IN ({','.join('?' for _ in list_table)}) ORDER BY Firm", (list_table))"
How do you use f-string in multiple lines?
Granny Aching
1,4352 gold badges16 silver badges38 bronze badges
asked Jan 9, 2019 at 18:59
Learner132
1351 gold badge3 silver badges10 bronze badges
-
Never use f-string for building SQL queries. See for example this: github.com/arogozhnikov/python3_with_pleasure/issues/3Francisco Puga– Francisco Puga2021年06月30日 11:46:23 +00:00Commented Jun 30, 2021 at 11:46
1 Answer 1
The issue stems from essentially invalid multi lines and not really any fault of f-string.. Note that you only need to use your variables inside the {} parenthesis and not pass them separately with f-string syntax. Use triple quotes or backslashes at end of strings to use multi line strings. Link to docs
test = "this is a
bad multiline " #Raises SyntaxError: EOL while scanning string literal
test = ''' this is a
valid multiline'''
a = "answer"
print(f'''test
is complete. check {a} ''')
answered Jan 9, 2019 at 19:08
Paritosh Singh
6,2662 gold badges17 silver badges35 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
default