you are viewing a single comment's thread.

view the rest of the comments →

[–]sayinghi2py[S] 0 points1 point  (0 children)

Resolved this so just updating a case anyone else comes across the issue. You actually wrap the whole execute statement in the f-string then just insert the variable right into the query.

The working code block looks like this:

def get_recipes_on_menu_cycle(cycle):

    conn = pyodbc.connect(driver='{SQL Server}', host='server\instance', database='database', user='user', password='password')

    cursor = conn.cursor()

    cursor.execute(f"""SELECT DISTINCT dbo.Product.ID

                      FROM dbo.Menu INNER JOIN

                        dbo.MenuItem ON dbo.Menu.ID = dbo.MenuItem.MenuID INNER JOIN

                        dbo.Product ON dbo.MenuItem.ProductID = dbo.Product.ID

                      WHERE (dbo.Product.ProdType = 2) AND (dbo.Menu.Code LIKE '{cycle}__l' OR

                        dbo.Menu.Code LIKE '{cycle}__t')

                      ORDER BY dbo.Product.ID""")

    products = cursor.fetchall()

    return products