[Python] Python pg8000 query params...syntax error at or near \”$2\"

Discussão em 'Python' iniciado por Stack, Abril 29, 2025.

  1. Stack

    Stack Membro Participativo

    Hi…I’m trying to add a parameter to my query but I get this error:

    "errorMessage": "{'S': 'ERROR', 'V': 'ERROR', 'C': '42601', 'M': 'syntax error at or near \”$2\"', 'P': '3793', 'F': 'scan.l', 'L': '1146', 'R': 'scanner_yyerror'}"


    This works:

    import pg8000

    account_id = 1234

    sql = “”"
    SELECT *
    FROM samples
    WHERE account_id = %s
    AND delete_date IS NULL
    ORDER BY date DESC
    “”"

    cursor.execute(sql, (account_id,))


    But this does not:

    import pg8000

    account_id = 1234

    start_date = query_string_params['start-date'] if 'start-date' in query_string_params else None

    // start_date format is: '2025-02-04'

    filters = “"
    if start_date is not None:
    filters = filters + f" AND DATE(sample_date) >= '{start_date}'"

    sql = “”"
    SELECT *
    FROM samples
    WHERE account_id = %s
    AND delete_date IS NULL
    %s
    ORDER BY date DESC
    “”"

    cursor.execute(sql, (account_id, filters))


    Any idea what I’m doing wrong?

    Continue reading...

Compartilhe esta Página