TypeError(“'NoneType'对象没有属性'__getitem__'”)

时间:2016-05-22 08:06:52

标签: python html mysql

我正在使用python 2.7

我必须使用python

连接mysql数据库和html表单页面

enter image description here

当我输入所有这样的空格时 enter image description here

表单给了我正确的答案,但是当我这样做的时候 enter image description here

它给了我这个错误

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

即使我没有填写所有字段,我如何才能获得正确的结果?

我的代码:

 # ----- 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,)]

# -*- 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 个答案:

答案 0 :(得分:0)

该错误表示您正在尝试索引None,如果renderTable(tuples)tuples,则None最有可能发生这种情况:

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

如果找不到任何内容,app.searchSong()可能会None返回IndexError。如果它返回一个空序列,你会得到一个web_view.getEngine().locationProperty().addListener(new ChangeListener<String>() { @Override public void changed(ObservableValue<? extends String> observableValue, String oldLoc, String newLoc) { String downloadableExtension = null; // todo I wonder how to find out from WebView which documents it could not process so that I could trigger a save as for them? String[] downloadableExtensions = { ".csv", ".csv?"}; logger.info("Site changed to " + newLoc); for (String ext: downloadableExtensions) { if (newLoc.endsWith(ext)) { downloadableExtension = ext; if(downloadableExtension.equals(".csv?")) { downloadableExtension = ".csv"; } logger.info("File is " + downloadableExtension); break; } } if (downloadableExtension != null) { logger.info("Starting download process for " + newLoc); // create a file save option for performing a download. int filenameIdx = newLoc.lastIndexOf("/") + 1; if (filenameIdx != 0) { File saveFile = new File("data/lastdownload" + downloadableExtension); if (saveFile != null) { BufferedInputStream is = null; BufferedOutputStream os = null; try { is = new BufferedInputStream(new URL(newLoc).openStream()); os = new BufferedOutputStream(new FileOutputStream(saveFile)); int b = is.read(); while (b != -1) { os.write(b); b = is.read(); } } catch (FileNotFoundException e) { System.out.println("Unable to save file: " + e); } catch (MalformedURLException e) { System.out.println("Unable to save file: " + e); } catch (IOException e) { System.out.println("Unable to save file: " + e); } finally { try { if (is != null) is.close(); } catch (IOException e) { /** no action required. */ } try { if (os != null) os.close(); } catch (IOException e) { /** no action required. */ } } } 。当搜索没有返回结果时,您需要处理这种情况。