0

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.

9
  • I didn't think mysql-connector supported named parameters, just %s. Commented Feb 11 at 1:30
  • Parameters should be allowed anywhere that expressions are allowed. That includes INTERVAL expressions. Commented Feb 11 at 1:31
  • The error message does not come from mysql, so the question is unlikely to be related to mysql. Commented Feb 11 at 1:41
  • @Barmar: Yes, named parameters: dev.mysql.com/doc/connector-python/en/… Commented Feb 11 at 2:11
  • Works for me using mysql-connector-python 9.1.0. What does python3 -m pip show mysql-connector-python output? Commented Feb 11 at 7:11

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.