如何解决MySQL/Python 中的“类型错误:格式字符串参数不足”
第一种类型有效
sql = """SELECT * FROM order_items WHERE (order_work_date between '%s' AND '%s') """ % (_startDate,_endDate)
第二种类型有效
sql = """SELECT * FROM order_items WHERE (order_client_company_name LIKE '%s') """ % ("%"+company_Name+"%")
第三种类型有错误信息“TypeError: not enough arguments for format string”
sql = """SELECT * FROM order_items WHERE (order_work_date between '%s' AND '%s') and (order_client_company_name LIKE '%s') """ % ((_startDate,_endDate),("%"+company_Name+"%"))
解决方法
格式化的所有参数都应该在一个元组中:
sql = """SELECT * FROM order_items WHERE (order_work_date between '%s' AND '%s') and (order_client_company_name LIKE '%s') """ % ((_startDate,_endDate,"%"+company_Name+"%"))
话虽如此,使用字符串格式从参数构建 SQL 字符串是一种不好的做法,因为它容易受到 SQL 注入攻击。您可能应该考虑使用绑定变量。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。