尝试自学Python。我有一个简单的Web应用程序,它获取表单字段的值,将其分配给名为“new_quote”的变量,并将其粘贴到SQLite表中的“content”字段中。
当我点击网络表单上的提交按钮时,我收到了一个TypeError ...
<type 'exceptions.TypeError'>: iteration over non-sequence
args = ('iteration over non-sequence',)
message = 'iteration over non-sequence'
它不喜欢下面这一行,但我不知道从哪里开始。
for each_item in new_quote:
用于将报价添加到DB的CGI脚本如下所示。任何建议将不胜感激。
#!/usr/bin/env python
import sqlite3
import cgi
import cgitb
cgitb.enable()
# This code takes the content of the form and assigns it to a variable called new_quotes.
form = cgi.FieldStorage()
new_quote = form.value[(0)]
# Items from new_quotes are pushed into the DB in the code below.
for each_item in new_quote:
connection = sqlite3.connect("quotes.sqlite")
cursor = connection.cursor()
content = new_quote[each_item].content
cursor.execute("INSERT INTO quotes(content) VALUES (?)")
connection.commit()
connection.close()
答案 0 :(得分:0)
您正在尝试迭代对象本身,这将返回错误。您想迭代对象内的列表,