9
with engine.connect() as con:

    rs = con.execute("""
                        SELECT  datediff(STR_TO_DATE(CONCAT(year,'-',month,'-',day), '%Y-%m-%d') , current_date()) 
                        from TABLE 
                        WHERE datediff(STR_TO_DATE(CONCAT(year,'-',month,'-',day), '%Y-%m-%d') , current_date()) < 900
                        group by STR_TO_DATE(CONCAT(year,'-',month,'-',day), '%Y-%m-%d');
                    """)

I feel the compiler is getting confused with '%Y-%m-%d', I might be wrong. Could someone help me on how to avoid this error:

Type Error:not enough arguments for format string

2
  • What are you using to connect to the mysql database? Which package? You might explain what you are trying to accomplish with your query. Commented Oct 18, 2016 at 20:50
  • using sqlalchemy and using MySQL Workbench Commented Oct 18, 2016 at 20:51

2 Answers 2

22

It sees your % signs and thinks you want to format the string. I believe you should be able to replace them with %% to indicate that you want the character, not a format substitution.

Sign up to request clarification or add additional context in comments.

1 Comment

Awesome answer.
4

You need to escape the %:

with engine.connect() as con:

    rs = con.execute("""
                        SELECT  datediff(STR_TO_DATE(CONCAT(year,'-',month,'-',day), '%%Y-%%m-%%d') , current_date()) 
                        from TABLE 
                        WHERE datediff(STR_TO_DATE(CONCAT(year,'-',month,'-',day), '%%Y-%%m-%%d') , current_date()) < 900
                        group by STR_TO_DATE(CONCAT(year,'-',month,'-',day), '%%Y-%%m-%%d');
                    """)

% is a reserved character in MySQL (wildcard).

Comments

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.