Question

I am trying to use the following query where I do some date_format manipulation and I would like also to use some placeholder. Unfortunately the query fails since it does not try to replace the placeholder. The query is the following:

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

and I am trying to use it through:

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

I am using mysqldb as library and Python 2.7 The Exception that I get is:

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

Any Ideas? Thank you

Was it helpful?

Solution

FIXED

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

By simply adding % in front of the one for the mysql db got it fixed ;)

Hope it may be helpful for others :D

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top