2015-09-01 203 views
0

我想在选择国家名称并按下按钮后获取国家/地区信息。我使用的是cherrypy,jinja,python和mysql。所以我希望信息在浏览器上显示。MySQL + Python索引超出范围错误

但是,当我按下提交按钮我得到这个错误:

countList=self.get_info(countryInfo)[0]  
IndexError: list index out of range 

这是导致该问题的代码的主要部分。

@cherrypy.expose 
    def get_Country_Info(self,countryInfo=None): 
     self.get_Names() 
     countList=self.get_info(countryInfo)[0] 
     tmpl=env.get_template("submit_test.html") 
     return tmpl.render(salutation="Welcome to", target="Country List", myList=self.country_list, country=countList) 

def get_info(self,countryInfo): 
     self.con.execute("SELECT Code, Name, Continent, Population, Capital FROM Country WHERE Name LIKE %s", (countryInfo,)) 
     self.countryDB=list(self.con.fetchall()) 
     return self.countryDB 
+1

的结果集由'self.con.fetchall()返回'空?如果你将一个空列表投给'list',它会失效'[]'并将其编入索引将超出范围 –

回答

0

您未正确调用execute

尝试: self.con.execute("SELECT Code, Name, Continent, Population, Capital FROM Country WHERE Name LIKE ?", countryInfo)

+0

谢谢,这有助于 –