TypeError: not enough arguments for format string - Python SQL connection while using %Y-%m

TypeError: not enough arguments for format string - Python SQL connection while using %Y-%m

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');
                    """)

我觉得编译器与“%Y-%m-%d”混淆了,我可能是错的。有人可以帮我避免这个错误吗:

Type Error:not enough arguments for format string

它看到您的 % 标志并认为您想要格式化字符串。我相信您应该能够将它们替换为 %% 以表明您想要字符,而不是格式替换。

您需要转义 %:

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');
                    """)

% 是 MySQL 中的保留字符(通配符)。