【问题标题】:MySQL+Python Index Out of Range ErrorMySQL+Python 索引超出范围错误
【发布时间】:2015-09-01 07:04:50
【问题描述】:

在选择国家名称并按下按钮后,我试图获取国家信息。我正在使用cherrypy、jinja、python和mysql。所以我希望信息显示在浏览器上。

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

    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

【问题讨论】:

  • self.con.fetchall()返回的结果集是空的吗?如果您将一个空列表投射到 list 它将成为 [] 并且索引这将超出范围

标签: python mysql cherrypy jinja2


【解决方案1】:

您没有正确调用execute

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

【讨论】:

    猜你喜欢
    • 2016-04-17
    • 2015-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 2021-05-12
    相关资源
    最近更新 更多