使用virtualenv與python3,Flask和sqlite3從非常基本的網頁查詢數據庫。我無法將輸入的數據插入主頁上的字段。 /what_the_f
頁面不斷顯示「失敗」。我究竟做錯了什麼?我懷疑try: cursor.excecute
,但我不知道爲什麼這不起作用以及如何解決這個問題,以便將輸入到home.html頁面的文本和整數插入到數據庫中。Flask sqlite3 INSERT失敗,沒有錯誤信息?
數據庫初始化文件:
import sqlite3
connection = sqlite3.connect('database.db')
connection.execute('CREATE TABLE movies (title TEXT, rating INTEGER)')
connection.close()
數據庫手動創建:
(venv) Tue Mar 07 18:42:38$ python initdb.py
的Python腳本:
from flask import Flask, render_template, request
import sqlite3
app = Flask(__name__)
@app.route('/')
def home():
return render_template('home.html')
@app.route('/what_the_f', methods = ['POST'])
def movie():
connection = sqlite3.connect('database.db')
cursor = connection.cursor # <-------------- LOOKIE THERE!
title = request.form['title']
rating = request.form['rating']
try:
cursor.execute('INSERT INTO movies (title,rating) VALUES (?,?)', (title,rating))
connection.commit()
message = "success"
except:
connection.rollback()
message = "failure"
finally:
connection.close()
return message
的/templates/home.html文件:
<!DOCTYPE html>
<html>
<head>
<title>WTF? TEST</title>
</head>
<body>
<h1>WTactualF??</h1>
<form action="/what_the_f" method="POST">
<input name="title" />
<input name="rating" />
<input type="submit" value="Submit" />
</form>
</body>
</html>
我只留下了從燒瓶服務器這份報告:
127.0.0.1 - - [07/Mar/2017 18:53:02] "POST /what_the_f HTTP/1.1" 200 -
在調試模式下運行瓶,我得到這個當我從服務器退出:
OSError: [Errno 9] Bad file descriptor