-1

I have a variable db which is read from environment file. I am tryin gto use it inside a cursor execution statements as below.

config={}
with open('environment.yml','r') as f:
    config = yaml.safe_load(f)
db = config['database_name']


result = cur.execute("TRUNCATE TABLE {db}.my_table")
result = cur.executemany('INSERT INTO {db}.my_table\
                             (id, description,code)\
                             VALUES (?, ?, ?)',
                             list(tuple(row) for row in co.values))

I have tried using however not sure if this is the right way to include variable in string.

queryString = f'''TRUNCATE TABLE {env_db}.smartselect_qc_ordercodes'''
result = cur.execute("TRUNCATE TABLE {db}.my_table")
result = cur.executemany('INSERT INTO {db}.my_table\
                             (id, description,code)\
                             VALUES (?, ?, ?)',
                             list(tuple(row) for row in co.values))
2
  • 1
    Use an f-string: f"TRUNCATE TABLE {db}.my_table"
    – Barmar
    Commented Nov 16, 2023 at 16:37
  • 2
    You can specify the default database name when you connect to the DB, so you shouldn't need to substitute it into every query.
    – Barmar
    Commented Nov 16, 2023 at 16:39

1 Answer 1

0
st = "TRUNCATE TABLE {db}.my_table".format(db=db)
1
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center. Commented Nov 16, 2023 at 18:35

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.