django.db.utils.ProgrammingError: not enough arguments for format string

雨点打透心脏的1/2处 2022-09-16 11:24 298阅读 0赞

django使用raw执行sql语句时报错

django.db.utils.ProgrammingError: not enough arguments for format string

sql语句

  1. data = gsm.NodeMessage.objects.raw(
  2. """select * from node where 1=1 and channel_name like '%新局%' ORDER BY device_code, channel_code, `order` asc""".format(conditions)
  3. )

原因

sql语句中含有“%”,这个“%”造成python认为此字符串有format倾向,出现错误。

解决方法

使用%%取代%可以解决这个问题
更正后:
sql语句

  1. data = gsm.NodeMessage.objects.raw(
  2. """select * from node where 1=1 and channel_name like '%%新局%%' ORDER BY device_code, channel_code, `order` asc""".format(conditions)
  3. )

后记

【后记】为了让大家能够轻松学编程,我创建了一个公众号【轻松学编程】,里面有让你快速学会编程的文章,当然也有一些干货提高你的编程水平,也有一些编程项目适合做一些课程设计等课题。

也可加我微信【1257309054】,拉你进群,大家一起交流学习。
如果文章对您有帮助,请我喝杯咖啡吧!

公众号

公众号

关注我,我们一起成长~~

发表评论

表情:
评论列表 (有 0 条评论,298人围观)

还没有评论,来说两句吧...

相关阅读