我正在尝试使用以下查询,我在那里做一些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>)
任何想法? 谢谢
答案 0 :(得分: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数据库的前面添加%,就可以修复它;)
希望它可能对其他人有所帮助:D