[Tutor] flask/sqlite3 problem - 'OperationalError: no such table'?

memilanuk memilanuk at gmail.com
Wed Sep 18 10:10:15 CEST 2013


Hello there,

I'm working thru a Flask tutorial, and when I get to the portion for 
querying the database (sqlite3) for the existing posts, i.e. 'SELECT * 
FROM posts', I get an error that says there is no such table 'posts' in 
my database.  Yet I can query said db file from either the sqlite3 
command-line client, or from the python interactive interpreter, and run 
the same query and get the expected results.  I've looked at this 'til 
I'm about ready to go cross-eyed... I'm sure it must be something simple 
that I'm missing, but I can't seem to spot it.  Below is the tail end of 
the traceback error message, followed by the code from the file in 
question.  Any help would be much appreciated.

Thanks,

Monte


Traceback (most recent call last):
...
     return test(*args, **kwargs)
   File 
"/home/monte/Dropbox/Documents/Programming/python/flask/blog/blog.py", 
line 57, in main
     cur = g.db.execute('SELECT * FROM posts')
OperationalError: no such table: posts




# blog.py - application module

from flask import Flask, render_template, request, session, flash, 
redirect, url_for, g
from functools import wraps
import sqlite3

# Configuration
DATABASE = 'fblog.db'
USERNAME = 'admin'
PASSWORD = 'admin'
SECRET_KEY = 'hard to guess'

app = Flask(__name__)

app.config.from_object(__name__)


def connect_db():
     #return sqlite3.connect(app.config['DATABASE'])
     return sqlite3.connect('fblog.db')

#### Other functions not shown for brevity ####

@app.route('/main')
@login_required
def main():
     g.db = connect_db()
     cur = g.db.execute('SELECT * FROM posts')
     posts = [dict(title=row[0], post=row[1]) for row in cur.fetchall()]
     g.db.close()
     return render_template('main.html', posts=posts)

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



More information about the Tutor mailing list