2013-01-11 101 views
0

我在用下面的代碼的麻煩:的Python unnable建立查詢字符串

start_date = dt.datetime(2012,01,01,00,00,00) 
end_date = dt.datetime(2014,01,01,00,00,00) 

sql="SELECT c.id extension, er.id, flow, filename, filesize, unread, er.cr_date, callerid, \ 
length, callid, info, party FROM extension_recording er, extension e, client c \ 
WHERE er.extension_id = e.id AND e.client_id = c.id AND c.parent_client_id = %s \ 
AND er.cr_date => %s AND er.cr_date <= %s" % (client_id) (start_date) (end_date) 

cur.execute(sql) 
recordings = cur.fetchall() 

if recordings.rowcount == 0: sys.exit("No recordings for selected date range - exiting") 

for recording in recordings: 
    do stuff to recording 

構建查詢字符串導致以下錯誤:

TypeError: 'str' object is not callable 

我敢肯定,我很想念一些非常明顯的東西,但我不能看到樹木的木材。此外

... er.cr_date <= %s" % (client_id, start_date, end_date) 

,而你在它,三重引號更方便排長隊:

+0

爲什麼(CLIENT_ID)( start_date)(end_date)? –

回答

4

你的長行的結尾改成這樣

sql = """ 
    SELECT c.id extension, ... 
    ... er.cr_date <= %s""" % (... 
+0

萬分感謝!仍在掙扎着Python語法。完美的作品。 – btongeorge