2017-07-06 26 views
0

此問題以前已被問過,但我沒有得到我出錯的地方。基本上我有一個多對多的表。和我在這,所以我寫了views.pay這樣序列項0:期望的str實例,找到的字節

def flightBooking(request): 
    if request.method=='POST': 
     form= SearchForm(request.POST) 
     if not form.is_valid(): 
      return render(request, 'booking/search.html',{'form':form}) 
     else: 
       form.is_valid() 
       data=(form.cleaned_data['fromLocation'],form.cleaned_data['toLocation']) 
       cursor=connection.cursor() 
       cursor.execute("""select flight.name,scheduleDepart.depart_time,scheduleArrive.arrival_time.ai.name from 
       flight flight,airline ai,flight_schedule scheduleDepart inner join 
       flight_schedule scheduleArrive on scheduleDepart.flight_id=scheduleArrive.flight_id where scheduleArrive.location_id=%d and scheduleDepart.location_id=%d, 
       and scheduleArrive.flight_id=flight.id and flight.airline_code_id=ai.id """,[data]) 
       results=dictFetchAll(cursor) 
       return render(results,'booking/searchResult.html') 
       #return HttpResponse(results,'booking/searchResult.html') 

def dictFetchAll(cursor): 
    "return all rows with column name" 
    columns=[col[0] for col in cursor.description] 
    return [ 
      dict(zip(columns,row)) 
      for row in cursor.fetchall() 
     ] 

這裏執行復雜查詢我從和位置是整場 ,我得到堆棧跟蹤這樣

File "C:\Program Files (x86)\lib\site-packages\django\core\handlers\exception.py", line 41, in inner 
    response = get_response(request) 
    File "C:\Program Files (x86)\lib\site-packages\django\core\handlers\base.py", line 187, in _get_response 
    response = self.process_exception_by_middleware(e, request) 
    File "C:\Program Files (x86)\lib\site-packages\django\core\handlers\base.py", line 185, in _get_response 
    response = wrapped_callback(request, *callback_args, **callback_kwargs) 
    File "E:\kepsla-workspace\WS\demoWS\flightBooking\booking\views.py", line 29, in flightBooking 
    and scheduleArrive.flight_id=flight.id and flight.airline_code_id=ai.id """,[data]) 
    File "C:\Program Files (x86)\lib\site-packages\django\db\backends\utils.py", line 80, in execute 
    return super(CursorDebugWrapper, self).execute(sql, params) 
    File "C:\Program Files (x86)\lib\site-packages\django\db\backends\utils.py", line 65, in execute 
    return self.cursor.execute(sql, params) 
    File "C:\Program Files (x86)\lib\site-packages\django\db\backends\mysql\base.py", line 101, in execute 
    return self.cursor.execute(query, args) 
    File "C:\Program Files (x86)\lib\site-packages\MySQLdb\cursors.py", line 234, in execute 
    args = tuple(map(db.literal, args)) 
    File "C:\Program Files (x86)\lib\site-packages\MySQLdb\connections.py", line 316, in literal 
    s = self.escape(o, self.encoders) 
    File "C:\Program Files (x86)\lib\site-packages\MySQLdb\converters.py", line 90, in quote_tuple 
    return "(%s)" % (','.join(escape_sequence(t, d))) 
TypeError: sequence item 0: expected str instance, bytes found 
[06/Jul/2017 22:45:52] "POST /booking HTTP/1.1" 500 97454 

所以任何人都請告訴我我哪裏錯了?或者有沒有其他的方式來寫這個查詢?

回答

0

我已經得到解決,從這樣的

select flight.name,scheduleDepart.depart_time,scheduleArrive.arrival_time.ai.name from 
       flight flight,airline ai,flight_schedule scheduleDepart inner join 
       flight_schedule scheduleArrive on scheduleDepart.flight_id=scheduleArrive.flight_id where scheduleArrive.location_id=%d and scheduleDepart.location_id=%d, 
       and scheduleArrive.flight_id=flight.id and flight.airline_code_id=ai.id """.join(''.join(elems) for elems in data 
相關問題