-2

I have a query that have to get as params month.

Query below work fine and filter based on first month.

        """SELECT i.date, i.title, i.img, i.video FROM table as i """
                """WHERE MONTH(STR_TO_DATE(i.date, "%M %D %Y") ) = 1  """ \
                """ORDER BY (i.id) desc""")

When tryig to put month as parameter im receiving a error

TypeError: 'int' object is not iterable

     """SELECT i.date, i.title, i.img, i.video FROM table as i """
                """WHERE MONTH(STR_TO_DATE(i.date, "%M %D %Y") ) = %s  """ \      
                """ORDER BY (i.id) desc""", (1))

Have tried with some brackets (1), (1,), [1] but wont work.

In sql the query work fine.

Random Davis
  • 6,662
  • 4
  • 14
  • 24
Stefani Toto
  • 280
  • 3
  • 13
  • Can you share how you're calling it in python ? – Brad Figueroa Jan 08 '21 at 22:59
  • Yes, please show a [mre] as well as the full stacktrace. There's not enough info in your post for us to be able to help you besides making a wild guess. – Random Davis Jan 08 '21 at 23:00
  • 1
    Does this answer your question? [How do I escape % from python mysql query](https://stackoverflow.com/questions/3037581/how-do-i-escape-from-python-mysql-query) – Iain Shelvington Jan 08 '21 at 23:00
  • Have tried but wont work: cursor.execute( """SELECT i.date, i.title, i.img, i.video FROM table as i """ """WHERE MONTH(STR_TO_DATE(i.date, "%%Y-%%m-%%d") ) = %s """ORDER BY (i.id) desc""", (1)) – Stefani Toto Jan 08 '21 at 23:01
  • 2
    Pass the parameters as a tuple: `(1, )`. At the moment you are passing a single int and you should pass an iterable, a tuple with a single element needs a comma – Iain Shelvington Jan 08 '21 at 23:02
  • Unrelated, but why use `"""` and then still close & reopen the quotes on each line adding a line continuation character? The idea of `"""` is to *not* have to do that. – trincot Jan 08 '21 at 23:03
  • @IainShelvington yes. thanks. %% and tuple solve issue – Stefani Toto Jan 08 '21 at 23:06
  • Is this really how one writes readable code in Python (I don't know Python)? – Strawberry Jan 09 '21 at 08:40

1 Answers1

0

Changing % with %% at date and using tuple solve the issue (1,)

cursor.execute(
            """SELECT i.date, i.title, i.img, i.video FROM table as i """
            """WHERE MONTH(STR_TO_DATE(i.date, "%%M %%D %%Y") ) = %s  """ \    
            """ORDER BY (i.id) desc""", (1,))
Stefani Toto
  • 280
  • 3
  • 13