[Python] Where in a MySQL statement can I use pyformat variables/parameters?

Discussão em 'Python' iniciado por Stack, Fevereiro 11, 2025.

  1. Stack

    Stack Membro Participativo

    Here's an example that doesn't work:

    stmt = '''
    SELECT `date_new`, `event`
    FROM `events`
    WHERE `date_new` > TIMESTAMP(DATE_SUB(NOW(), INTERVAL %(days)s day))
    '''

    args = {'days': 30}

    c.execute(stmt, args)


    which gives me an error: ValueError: Could not process parameters.

    A query that includes something like this:

    WHERE quantity = %(qty)s

    works just fine.

    I did find this: What parts of a SQL query are allowed to be parameterized? But that leaves the question in the specific example above: What about the argument to INTERVAL makes it not work?

    I have found lots of documentation about the basics of using pyformat variables/parameters. I think part of the problem I'm having finding something is that it involves the intersection of Python and MySQL via mysql.connector.

    I'd like a pointer to some documentation about what parts of queries allow their use and what ones don't, specifically for MySQL.

    Continue reading...

Compartilhe esta Página