Forums

flask and sqlite error

Hi, i'm new to python web frameworks, but not so new to programming.

I got flaskr.py taken from tutorial https://github.com/mitsuhiko/flask/blob/master/examples/flaskr/flaskr.py

2012-08-28 17:47:13,311 :Traceback (most recent call last):
2012-08-28 17:47:13,312 :  File "/usr/local/lib/python2.6/dist-packages/Flask-0.9-py2.6.egg/flask/app.py", line 1701, in __call__
2012-08-28 17:47:13,312 :    return self.wsgi_app(environ, start_response)
2012-08-28 17:47:13,312 :  File "/usr/local/lib/python2.6/dist-packages/Flask-0.9-py2.6.egg/flask/app.py", line 1689, in wsgi_app
2012-08-28 17:47:13,313 :    response = self.make_response(self.handle_exception(e))
2012-08-28 17:47:13,313 :  File "/usr/local/lib/python2.6/dist-packages/Flask-0.9-py2.6.egg/flask/app.py", line 1687, in wsgi_app
2012-08-28 17:47:13,313 :    response = self.full_dispatch_request()
2012-08-28 17:47:13,313 :  File "/usr/local/lib/python2.6/dist-packages/Flask-0.9-py2.6.egg/flask/app.py", line 1360, in full_dispatch_request
2012-08-28 17:47:13,314 :    rv = self.handle_user_exception(e)
2012-08-28 17:47:13,314 :  File "/usr/local/lib/python2.6/dist-packages/Flask-0.9-py2.6.egg/flask/app.py", line 1358, in full_dispatch_request
2012-08-28 17:47:13,314 :    rv = self.dispatch_request()
2012-08-28 17:47:13,314 :  File "/usr/local/lib/python2.6/dist-packages/Flask-0.9-py2.6.egg/flask/app.py", line 1344, in dispatch_request
2012-08-28 17:47:13,315 :    return self.view_functions[rule.endpoint](**req.view_args)
2012-08-28 17:47:13,315 :  File "/home/sztosz/flaskr/flaskr.py", line 62, in show_entries
2012-08-28 17:47:13,316 :    
2012-08-28 17:47:13,316 :sqlite3.OperationalError: no such table: entries

So basically my app can't read the db, or read it and yet somehow it can't seem to find no tables in it. I try bash:

17:50 ~ $ cd flaskr/
17:50 ~/flaskr $ ls
flaskr.db flaskr.py flaskr.pyc static templates
17:50 ~/flaskr $ sqlite3 flaskr.db
SQLite version 3.7.3
 Enter ".help" for instructions
 Enter SQL statements terminated with a ";"
 sqlite> select title, text from entries;
 FIRST|First entry
 SECOND|second entry

 sqlite>

Database exist, and it is populated, miracle :) I try Python console:

>>> db = sqlite3.connect('/home/sztosz/flaskr/flaskr.db')
>>> db
<sqlite3.Connection object at 0x190a4b8>
>>> data = db.execute('select title, text from entries')
>>> data
<sqlite3.Cursor object at 0x16d2030>
>>> for x in data.fetchall():
...     print x
...
(u'FIRST', u'First entry')
(u'SECOND', u'second entry\r\n')
>>>

Another miracle :)

My question is: why is it not working normally???

http://sztosz.pythonanywhere.com/

Hi sztosz -- have you set the DATABASE variable in your flaskr.py to /home/sztosz/flaskr/flaskr.db, like you did in your Python console? If you just used flaskr.db like you did from the bash command line then it wouldn't be able to find the database, and you might get an error like the one you're seeing.

Yes, it looks like this:

# configuration
DATABASE = '/home/sztosz/flaskr/flaskr.db'
DEBUG = True
SECRET_KEY = 'development key'
USERNAME = 'admin'
PASSWORD = 'default'

# create our little application :)
app = Flask(__name__)
app.config.from_object(__name__)
app.config.from_envvar('FLASKR_SETTINGS', silent=True)

And I tried doing it in different folders, switching it's place etc... it does not simply work, the error message is the same.

UPDATE:

2012-08-28 20:34:13,815 :  File "/home/sztosz/flaskr/flaskr.py", line 21, in debug
2012-08-28 20:34:13,816 :    db = sqlite3.connect(app)
2012-08-28 20:34:13,816 :ValueError: database parameter must be string or APSW Connection object

All i have is simple: db = sqlite3.connect(app) as the only thing the app really do... and i fail, is it an encoding problem or something else?

That

db = sqlite3.connect(app)

in the stack trace looks wrong. Shouldn't it be

db = sqlite3.connect(app.config['DATABASE'])

?

Strangely enough i got to results, code below now works (thanks glenn for pointin this obvious mistake):

@app.route('/')
def debug():
    db = sqlite3.connect(app.config['DATABASE'])
    cur = db.execute('select title from entries')
    ret = ''
    for x in cur.fetchall():
        ret += str(x)
    return x

I need some sleep, i'll investigate rest on morrow :)

Now it works as intended, at the time of my tests at work i was not aware, that one has to read FAQ before asking questions of forum.

Do I have to restart the web server whenever I change my code?

Well, after reading FAQ i know now, that i have to restart server. Thank you for all the help.

We never have to read the FAQ before posting a question on a forum, but it is Frequently a good idea...☺

Glad it's sorted! Perhaps we should do one of those things some sites do, where we use your forum post as a search query across the FAQ when you try to post something and make suggestions before posting it. Or maybe not, those are generally irritating and rarely suggest anything useful...

I'd just love to be able to search the forum posts.

Also, the ability to link (in the list of it's topic) to an individual post would rock!

It's already possible to link to individual posts (see this link, for example), so I wouldn't have thought it was tricky to implement. Probably just a case of there being quite a lot of substantially more voluminous aquatic fauna to sauté before looking after the finer points of the forum.

Still, I'm sure it'll go onto the ever growing list of requests! (^_^)

Sure, we can read the source and manually link to individual posts, but having tools to do it will be the only way it will make sense for real use. But, yes you are correct. If we know where to locate what we want to reference we can do it manually. I'd just prefer to see the two tools that make it practical.

1. Search functionality.
2. Post ID reference link functionality.

Also, I agree. PA has a lot on their plate and I fully understand why these features haven't made it to the top of the list. Although I must admit...I do wonder why we don't use an existing forum technology with all the bells & whistles versus having to roll them as needed...