OperationalError:没有这样的表:学生

时间:2016-08-05 00:41:12

标签: python html sqlite templates flask

作为初学者,我一直在url:http://www.tutorialspoint.com/flask/flask_sqlite.htm尝试使用tutorialpoint的教程。

然而,在此错误之后

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>  

但是,我认为可能还有其他错误。

现在,我收到以下错误:

  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>

Flask-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 个答案:

答案 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()