gpt4 book ai didi

python - TypeError ("' NoneType'对象没有属性 '__getitem__'“,)

转载 作者:行者123 更新时间:2023-11-29 20:59:18 26 4
gpt4 key购买 nike

我正在使用 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)

最佳答案

该错误表明您正在尝试索引 None ,这最有可能发生在 renderTable(tuples)如果tuplesNone :

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

可能app.searchSong()返回None万一什么也没找到。如果它返回一个空序列,您将得到 IndexError反而。当搜索没有返回结果时,您需要处理这种情况。

关于python - TypeError ("' NoneType'对象没有属性 '__getitem__'“,),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37371951/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com