2016-05-22 20 views
0

ich bin mit Python 2.7Typeerror (" 'NoneType' Objekt hat kein Attribut '__getitem__'",)

Ich habe eine MySQL-Datenbank und eine HTML-Formular Seite verbinden mit Python

enter image description here

, wenn ich in allen Räumen wie diese enter image description here

Typen gibt das Formular, um mir die richtigen Antworten, aber wenn, wenn ich es tun wie diese enter image description here

Es gibt mir diesen Fehler

TypeError("'NoneType' object has no attribute '__getitem__'",) 

Wie kann ich die richtigen Ergebnisse nehmen, auch wenn ich nicht alle Felder ausfüllen bin?

Mein Code:

# ----- CONFIGURE YOUR EDITOR TO USE 4 SPACES PER TAB ----- # 
# -*- coding: utf-8 -*- 
#!/usr/bin/python 
import pymysql as db 
import settings 

def connection(): 
    ''' Use this function to create your connections ''' 
    con = db.connect(
     settings.mysql_host, 
     settings.mysql_user, 
     settings.mysql_passwd, 
     settings.mysql_schema, 
     charset='utf8', 
     use_unicode=True) 

    return con 
def searchSong(titlos,etos_par,etaireia): 


    # Create a new connection 
    con=connection() 

    #create a cursor to the connection 
    cur=con.cursor() 
    cur.execute ("SET NAMES 'utf8'"); 
    cur.execute ("SET CHARACTER SET 'utf8'"); 

    cur.execute("SELECT tragoudi.titlos, tragoudi.etos_par, cd_production.etaireia FROM tragoudi JOIN singer_prod ON tragoudi.titlos=singer_prod.title JOIN cd_production ON singer_prod.cd=cd_production.code_cd GROUP BY tragoudi.titlos HAVING tragoudi.titlos LIKE %s AND tragoudi.etos_par LIKE %s AND cd_production.etaireia LIKE %s",(titlos,etos_par,etaireia)) 
    con.commit() 




    for row in cur.fetchall(): 
     return [(row,)] 

UND

# -*- coding: utf-8 -*- 
#!/usr/bin/python 
import sys, os 
sys.path.append(os.path.join(os.path.split(os.path.abspath(__file__))[0], 'lib')) 
from bottle import route, run, static_file, request 
import pymysql as db 
import settings 
import app 

def renderTable(tuples): 
    printResult = """<style type='text/css'> h1 {color:red;} h2 {color:blue;} p {color:green;} </style> 
    <table border = '1' frame = 'above'>""" 

    header='<tr><th>'+'</th><th>'.join([str(x) for x in tuples[0]])+'</th></tr>' 
    data='<tr>'+'</tr><tr>'.join(['<td>'+'</td><td>'.join([str(y) for y in row])+'</td>' for row in tuples[1:]])+'</tr>' 

    printResult += header+data+"</table>" 
    return printResult 
@route('/searchSong') 
def searchSongWEB(): 
    titlos = request.query.titlos 
    etos_par = request.query.etos_par 
    etaireia = request.query.etaireia 

    table = app.searchSong(titlos,etos_par,etaireia) 
    print "<html><body>" + renderTable(table) + "</body></html>" 
    return "<html><body>" + renderTable(table) + "</body></html>" 


@route('/:path') 
def callback(path): 
    return static_file(path, 'isto') 

@route('/') 
def callback(): 
    return static_file("index.html", 'isto') 


run(host='localhost', port=settings.web_port, reloader=True, debug=True) 
+1

Welche Zeile Ihres Codes gibt diesen Fehler? –

+0

Sie sollten wirklich Vorlagen verwenden, anstatt die HTML-Strings selbst zu erstellen. –

Antwort

0

Der Fehler zeigt an, dass Sie indizieren in None sind versucht, die meisten geschieht wahrscheinlich in renderTable(tuples) wenn tuples ist None:

header='<tr><th>'+'</th><th>'.join([str(x) for x in tuples[0]])+'</th></tr>' 
#            here! ---^ 

Wahrscheinlich app.searchSong() gibt None zurück, falls es nichts gefunden hat. Wenn es eine leere Sequenz zurückgibt, erhalten Sie stattdessen eine IndexError. Sie müssen den Fall behandeln, wenn die Suche keine Ergebnisse liefert.

Verwandte Themen