F string assignment from a different file?

Viewed 340

I like to keep one file with just my f strings that represents SQL queries and each query needs a db name. In my python program, i will populate the db variable as I am reading out db names.

E.g. in queries.py, i have

query_1 = f"select * from {db} limit 10;"

in main.py, i use

from quries import quries
# read out db information into dbs
for db in dbs:
  # execute the query_1 for each db 

How can I achieve the logic in the loop?

1 Answers

Don't use an f-string, but .format()

# queries
query_1 = "select * from {db} limit 10;"
from queries import query_1
...
for db in dbs:
    query = query_1.format(db=db)
    ...
Related