2016-08-05 90 views
0

作爲初學者,我一直在url上嘗試tutorialpoint教程中的瓶子:http://www.tutorialspoint.com/flask/flask_sqlite.htmOperationalError:沒有這樣的表格:學生

但是,此錯誤後

jinja2.exceptions.TemplateNotFound TemplateNotFound:home.html的

這應該顯示: 1.添加新的記錄 2.顯示列表

我做的如下:

<h3>Students (<a href = "{{ url_for('new_student') }}">Add new record 
    </a>)</h3> 

    <h3> (<a href = "{{ url_for('list') }}">Show List 
    </a>)</h3> 

但是,我認爲可能有其他的錯誤RS。

現在,我收到以下錯誤:

sqlite3.OperationalError 
    OperationalError: no such table: students 

誰能幫助教程工作?非常感激。 * ps:請不要標記這篇文章。如果你不能或不想幫忙。請將此問題留給願意幫助初學者的人。謝謝。

編號: http://www.tutorialspoint.com/flask/flask_sqlite.htm

這裏是複製粘貼的要求由@Wayne:

'student.html':

<html> 
    <body> 

     <form action = "{{ url_for('addrec') }}" method = "POST"> 
     <h3>Student Information</h3> 
     Name<br> 
     <input type = "text" name = "nm" /></br> 

     Address<br> 
     <textarea name = "add" ></textarea><br> 

     City<br> 
     <input type = "text" name = "city" /><br> 

     PINCODE<br> 
     <input type = "text" name = "pin" /><br> 
     <input type = "submit" value = "submit" /><br> 
     </form> 

    </body> 
</html> 

result.html:

<!doctype html> 

    <html> 
     <body> 

      result of addition : {{ msg }} 
      <h2><a href = "\">go back to home page</a></h2> 

     </body> 
    </html> 

list.html:

<!doctype html> 
<html> 
    <body> 

     <table border = 1> 
     <thead> 
      <td>Name</td> 
      <td>Address>/td< 
      <td>city</td> 
      <td>Pincode</td> 
     </thead> 

     {% for row in rows %} 
      <tr> 
       <td>{{row["name"]}}</td> 
       <td>{{row["addr"]}}</td> 
       <td> {{ row["city"]}}</td> 
       <td>{{row['pin']}}</td> 
      </tr> 
     {% endfor %} 
     </table> 

     <a href = "/">Go back to home page</a> 

    </body> 
</html> 

瓶,SQLite的應用程序:

from flask import Flask, render_template, request 
import sqlite3 as sql 
app = Flask(__name__) 

@app.route('/') 
def home(): 
    return render_template('home.html') 

@app.route('/enternew') 
def new_student(): 
    return render_template('student.html') 

@app.route('/addrec',methods = ['POST', 'GET']) 
def addrec(): 
    if request.method == 'POST': 
     try: 
     nm = request.form['nm'] 
     addr = request.form['add'] 
     city = request.form['city'] 
     pin = request.form['pin'] 

     with sql.connect("database.db") as con: 
      cur = con.cursor() 

      cur.execute("INSERT INTO students (name,addr,city,pin) 
       VALUES (?,?,?,?)",(nm,addr,city,pin)) 

      con.commit() 
      msg = "Record successfully added" 
     except: 
     con.rollback() 
     msg = "error in insert operation" 

     finally: 
     return render_template("result.html",msg = msg) 
     con.close() 

@app.route('/list') 
def list(): 
    con = sql.connect("database.db") 
    con.row_factory = sql.Row 

    cur = con.cursor() 
    cur.execute("select * from students") 

    rows = cur.fetchall(); 
    return render_template("list.html",rows = rows) 

if __name__ == '__main__': 
    app.run(debug = True) 
+1

讓您擁有一個[MCVE] –

+0

確保您所創建的學生表和數據庫文件是相同的,請添加代碼的其餘部分的足夠。如有疑問,請使用絕對路徑。 –

回答

0

有上應清楚地註明運行,以建立數據庫另一個代碼一個錯字。首先運行它,它現在應該工作。

import sqlite3 

conn = sqlite3.connect('database.db') # formerly typo as 'databsae.db' 
print "Opened database successfully"; 

conn.execute('CREATE TABLE students (name TEXT, addr TEXT, city TEXT, pin TEXT)') 
print "Table created successfully"; 
conn.close() 
相關問題