пиодбк.Ошибка программирования: («SQL содержит 2 маркера параметров, но был указан 1 параметр», «HY000»)

#python #sql #sql-server #pyodbc

Вопрос:

при попытке запустить свой код на тестовом сервере я получаю:

 Callback error updating downtime.children, downtime_data.data09:11:27
Callback error updating downtime.children, downtime_data.data
 

И:

 Traceback (most recent call last):
  File "C:Usersdenis.akvicDesktopflaskappsapp7.py", line 302, in fetch_the_downtime
    records = mssql_conn.execute_query(query_string, parameters, str(database))
  File "C:Usersdenis.akvicDesktopflaskmssql_conn.py", line 63, in execute_query
    cursor.execute(query_string, parameters)
pyodbc.ProgrammingError: ('The SQL contains 2 parameter markers, but 1 parameters were supplied', 'HY000')
 
 @app.callback([
    Output("downtime", "children"),
    Output("downtime_data","data")
    ],
        [Input("submit_button", "n_clicks")],
        state=[State("start_date_picker", "date"),
        State("start_time_picker", "value"),
        State("end_date_picker", "date"),
        State("end_time_picker", "value"),
        State("database", "value"),
        State("language", "value")]
)
        
def fetch_the_downtime(n_clicks, start_date_picker, start_time_picker, end_date_picker, end_time_picker, database, language):
    if n_clicks:
        start_time = datetime.datetime.strptime(start_date_picker, "%Y-%m-%d")   datetime.timedelta(hours=int(start_time_picker[0:2]))
        end_time = datetime.datetime.strptime(end_date_picker, "%Y-%m-%d")   datetime.timedelta(hours=int(end_time_picker[0:2]))
        parameters = [start_time, end_time],

        query_string = mssql_queries.build_db_query_app7(str(language))
        records = mssql_conn.execute_query(query_string, parameters, str(database))

        if records:
            dataframe = pd.DataFrame.from_records(records)
            dataframe.columns = ["ID", "DAY", "MONTH", "Duration", "Week Number"]
            return dbc.Table.from_dataframe(dataframe), dataframe.to_json(date_format="iso", orient="split")
        else:
            return None, None, dbc.Alert("No entries within the specified timeframe,")
    else:
        return None, None, None 



@app.callback(
    [Output("downtime_data", "children"),
    Output("Report_link", "data")],
    [
        Input("downtime", "children")
    ],
)
def make_table(n_clicks,start_date_picker, start_time_picker, end_date_picker, end_time_picker, database, language):
    if n_clicks:
        start_time_picker = datetime.datetime.strptime(start_date_picker, "%Y-%m-%d")   datetime.timedelta(hours=int(start_time_picker[0:2]))
        end_time_picker = datetime.datetime.strptime(end_date_picker, "%Y-%m-%d")   datetime.timedelta(hours=int(end_time_picker[0:2]))
        databse= [start_time_picker, end_time_picker],
    if not n_clicks:
        db_query_string = """"SELECT NotificationLog.ID as 'ID',
                        DAY(Timestamp) as 'DAY',
                        MONTH(Timestamp) as 'MONTH',
                        NotificationLog.Duration,
                        DATEPART(WEEK, NotificationLog.Timestamp)-1 as 'Week Number'
                        FROM NotificationLog
                            BETWEEN ? AND ?"""

        records = mssql_conn.execute_without_params(query_string=db_query_string, db_name="ked33")

        if records:
            dataframe = pd.DataFrame.from_records(records)
            dataframe.columns = ["ID", "DAY", "MONTH", "Duration", "Week Number"]
            return dbc.Table.from_dataframe(dataframe), dataframe.to_json(date_format='iso', orient='split')
        else:
            return None, None
    else:
        return None, None
 

Это мой вопрос:

 def build_db_query_app7(language):
    db_query_string = """SELECT NotificationLog.ID as 'ID',
                        DAY(Timestamp) as 'DAY',
                        MONTH(Timestamp) as 'MONTH',
                        NotificationLog.Duration,
                        DATEPART(WEEK, NotificationLog.Timestamp)-1 as 'Week Number'
                        FROM NotificationLog
                            BETWEEN ? AND ?""".format(language)
    return db_query_string
 

Комментарии:

1. Что такое функция mssql_conn.execute_without_params? BETWEEN ? AND ? Часть db_query_string содержит два маркера параметров (два вопросительных знака), но cursor.execute предоставляет только одно значение для этих двух маркеров.