2013-07-11 45 views
0

我想使用下面的查詢,我做了一些date_format操作,我也想用一些佔位符。不幸的是,查詢失敗,因爲它不會取代佔位符。該查詢如下:Python MySQL - 帶參數和其他佔位符的mysqldb查詢

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

固定

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