<type'exception.nameerror'=“”>

时间:2016-04-21 20:37:43

标签: python mysql web-applications nameerror

尝试在MA中创建一个带有远足路径列表的Web应用程序(使用mySQL和Python)。我只想在一个页面上显示我的数据库中的所有路径名称,并且无法弄清楚为什么没有显示任何内容:

################################################################################
def getAllHikes():
"""
This is a middleware function to read from the database.
It returns a list containing records of all trails we have in the table.
"""

# connect to db
conn, cursor = getConnectionAndCursor()

# prepare SQL
sql = """
SELECT name
FROM hiking
"""

# run the SQL
cursor.execute(sql)

# fetch the results
data = cursor.fetchall()

# clean up
cursor.close()
conn.close()

return data

################################################################################
def showAllHikes(data):
'''Produce a table showing all trails, one per row.'''

print('''
Here are all the popular trails we have on file:
<table>
    <tr>
      <td>%s</td>
      <td>%s</td>
      <td>%s</td>
      <td>%s</td>
      <td>%s</td>
    </tr>
      ''') % (name, town)

print('''
</table>
''')
################################################################################
if __name__ == "__main__":

# get form field data
form = cgi.FieldStorage()

doHTMLHead("MassHike: A database of popular Massachusetts trails")

data = getAllHikes()
showAllHikes(data)


doHTMLTail()

当我尝试使用Web应用程序时,我总是遇到这个问题。我收到错误消息 args =(“未定义全局名称'名称',”)

如果你能用非常简单的术语解释我会很感激。我根本不明白这一点。谢谢!

1 个答案:

答案 0 :(得分:1)

问题在于以下方法。您未在函数内或name级别定义townglobal

def showAllHikes(data):
    '''Produce a table showing all trails, one per row.'''

    print('''
    Here are all the popular trails we have on file:
    <table>
        <tr>
          <td>%s</td>
          <td>%s</td>
          <td>%s</td>
          <td>%s</td>
          <td>%s</td>
        </tr>
          ''') % (name, town)

    print('''
    </table>
    ''')

要获取此信息,您需要查看data list tuple} print(''' Here are all the popular trails we have on file: <table>''') for name, town in data: print(''' <tr> <td>%s</td> <td>%s</td> </tr> ''') % (name, town) print(''' </table> ''') {/ 1}}。

有了这些信息,我们可以做到以下几点:

sql = """SELECT name FROM hiking"""

但是,看起来你的sql查询需要包含城镇信息,因为它目前只有Traceback (most recent call last): File "<console>", line 1, in <module> ValueError: need more than 1 value to unpack ,所以这很可能会失败:

[s,e] = regexp(inputString,'(1+)');
maximumSequenceLength = max(e-s+1);