0

我正在尝试使用以下查询来进行一些 date_format 操作,并且我还想使用一些占位符。不幸的是,查询失败,因为它没有尝试替换占位符。查询如下:

query_with_parameters =  """select ID, IP_src, IP_dst, sum(bytes) as Total_Bytes, count(*) as total_packets,\
 ( sum(bytes) / count(*) )  as Average, date_format(date, "%H") as Hour , date_format(date, "%d %m %Y")\
  as date from Packets where date BETWEEN %s and %s group by IP_src,\
   IP_dst, Hour, date order by IP_src, IP_dst, Hour, date;"""

我正在尝试通过以下方式使用它:

cursor.execute(query_with_parameters, ('2013-06-30 00:00:00' , '2013-06-30 23:59:59'))

我使用 mysqldb 作为库和 Python 2.7 我得到的异常是:

(<type 'exceptions.ValueError'>, ValueError("unsupported format character 'H' (0x48) at index 144",), <traceback object at 0x33447a0>)

有任何想法吗?谢谢

4

1 回答 1

0

固定的

query_with_parameters =  """select ID, IP_src, IP_dst, sum(bytes) as Total_Bytes, count(*) as total_packets, ( sum(bytes) / count(*) )\
  as Average, date_format(date, "%%H") as Hour , date_format(date, "%%d %%m %%Y") as date_n from Packets\
   where date BETWEEN %s and %s group by IP_src, IP_dst, Hour,\
    date_n order by IP_src, IP_dst, Hour, date_n;"""

只需在 mysql db 的前面添加 % 就可以修复它;)

希望它对其他人有帮助:D

于 2013-07-11T16:15:16.847 回答