62,614
社区成员
发帖
与我相关
我的任务
分享
在python中导入sql语句总是报错,最近新上手,有没有大佬帮忙分析一下 queries={ 'Inter_in_flow': 'SELECT h.name hname,it.NAME itname,round(avg(tru.value_avg)/1000/1000,5) vel FROM items it,trends_uint tru,hosts h WHERE it.itemid = tru.itemid AND h.name in (select name from hosts where (name like '%%%%-Spoke-%%%%' OR name like '%%%%-Hub-%%%%') status=0 AND h.hostid = it.hostid AND it.NAME REGEXP '【Internet】' AND it.NAME REGEXP '【Inbound Flow】Monitor$' AND tru.clock < unix_timestamp('2023-01-01') and tru.clock > unix_timestamp('2022-12-01') group by itname,hname order by hname;' 'Inter_out_flow':'SELECT h.name hname,it.NAME itname,round(avg(tru.value_avg)/1000/1000,5) vel FROM items it,trends_uint tru,hosts h WHERE it.itemid = tru.itemid AND h.name in (select name from hosts where (name like '%%%%-Spoke-%%%%' OR name like '%%%%-Hub-%%%%') and status=0) AND h.hostid = it.hostid AND it.NAME REGEXP '【Internet】' AND it.NAME REGEXP '【Outbound Flow】Monitor$' AND tru.clock < unix_timestamp('2023-01-01') and tru.clock > unix_timestamp('2022-12-01') group by itname,hname order by hname;' }
在这个查询语句中,您使用了单引号'将字符串括起来,并且在字符串中使用了百分号%来进行模糊匹配,但是您还在字符串中使用了单引号,这可能导致查询语句的语法出现问题。
解决此问题的方法是将字符串括起来的方式从单引号'改为双引号",并使用转义字符"来转义字符串中的双引号。此外,在字符串中使用百分号%时,应该将其转义为两个百分号%%,以便正确地进行模糊匹配。
下面是修改后的查询语句:
queries={
"Inter_in_flow": "SELECT h.name hname,it.NAME itname,round(avg(tru.value_avg)/1000/1000,5) vel FROM items it,trends_uint tru,hosts h WHERE it.itemid = tru.itemid AND h.name in (select name from hosts where (name like '%%%%-Spoke-%%%%' OR name like '%%%%-Hub-%%%%') status=0 AND h.hostid = it.hostid AND it.NAME REGEXP '【Internet】' AND it.NAME REGEXP '【Inbound Flow】Monitor$' AND tru.clock < unix_timestamp('2023-01-01') and tru.clock > unix_timestamp('2022-12-01')) group by itname,hname order by hname;",
"Inter_out_flow":"SELECT h.name hname,it.NAME itname,round(avg(tru.value_avg)/1000/1000,5) vel FROM items it,trends_uint tru,hosts h WHERE it.itemid = tru.itemid AND h.name in (select name from hosts where (name like '%%%%-Spoke-%%%%' OR name like '%%%%-Hub-%%%%') and status=0) AND h.hostid = it.hostid AND it.NAME REGEXP '【Internet】' AND it.NAME REGEXP '【Outbound Flow】Monitor$' AND tru.clock < unix_timestamp('2023-01-01') and tru.clock > unix_timestamp('2022-12-01') group by itname,hname order by hname;"
}
在这个修改后的查询语句中,字符串使用双引号括起来,并且在字符串中的百分号使用了两个百分号%%来进行模糊匹配。