Re: [flask] Flask Tutorial sqlite3.ProgrammingError: Cannot operate
on a closed database.
- From:
- Rajiv Abraham
- Date:
- 2010-12-03 @ 18:23
Hi Didip,
Im sorry it was my mistake. The text db.commit() was incorrectly
indented. I have learned my python indentation lesson :)
Thank you for your quick response!
Rajiv
2010/12/2 Didip Kerabat <didipk@gmail.com>:
> Hi Rajiv,
>
>>
>> Environment: Ubuntu 10.10, Python 2.6.6
>>
>> I am following the tutorial on Step 3: Creating the database.
>> 1) I did not have sqlite3. So I installed using apt-get but outside
>> the virtual environment
>
> sqlite3 is already included as part of Python 2.6, so you shouldn't need
to do anything here.
>
>
>> 2) I then ran sqlite3 /tmp/flaskr.db < schema.sql. No error but no
>> flaskr.db created either.
>
> After you did sqlite3 /tmp/flaskr.db < schema.sql, can you do:
>
> sqlite3 /tmp/flaskr.db
>
> if successful, you should be getting sqlite3 prompt. If not, can you
show what your schema.sql look like?
>
> Also, this step is unnecessary if you wish to do init_db()
>
>> 3) I then copied the code in the tutorial regarding init_db(). I gave
>> my ubuntu usr/pwd. I ran it and I got the following error
>> =============ERROR MESSAGE================
>> (env)vijar82@ubuntu:~/workspace/python/flaskr$ python
>> Python 2.6.6 (r266:84292, Sep 15 2010, 15:52:39)
>> [GCC 4.4.5] on linux2
>> Type "help", "copyright", "credits" or "license" for more information.
>>>>> from flaskr import init_db
>> init>>> init_db()
>
> Is your schema.sql located here? ~/workspace/python/flaskr
>
>> Traceback (most recent call last):
>> File "<stdin>", line 1, in <module>
>> File "flaskr.py", line 26, in init_db
>> db.commit()
>> sqlite3.ProgrammingError: Cannot operate on a closed database.
>> =============ERROR MESSAGE================
>>
>> I then created an empty /tmp/flaskr.db but I got the same error.
>> Here is my flaskr.py.
>> ==================== CODE========================
>> # all the imports
>> from __future__ import with_statement
>> from contextlib import closing
>> import sqlite3
>> from flask import Flask, request, session, g, redirect, url_for, \
>> abort, render_template, flash
>>
>> # configuration
>> DATABASE = '/tmp/flaskr.db'
>> DEBUG = True
>> SECRET_KEY = 'development key'
>
>
>> USERNAME = 'Is this linux or db username'
>> PASSWORD = 'Is this linux or db pwd'
>>
>
> You don't need USERNAME and PASSWORD here.
>
> Also, there's nothing special about all these uppercase words. They are
just variables. That said, you should change your connect_db() to:
>
> def connect_db():
> return sqlite3.connect(DATABASE) # instead of
sqlite3.connect(app.config['DATABASE'])
>
>> # create our little application :)
>> app = Flask(__name__)
>> app.config.from_object(__name__)
>>
>> def connect_db():
>> return sqlite3.connect(app.config['DATABASE'])
>>
>> def init_db():
>> with closing(connect_db()) as db:
>> with app.open_resource('schema.sql') as f:
>> db.cursor().executescript(f.read())
>> db.commit()
>>
>>
>>
>>
>> if __name__ == '__main__':
>> app.run()
>
> Hope that helps.
>
> - Didip -
>
>
--
Take care,
Rajiv
Re: [flask] Flask Tutorial sqlite3.ProgrammingError: Cannot operate
on a closed database.
- From:
- Rajiv Abraham
- Date:
- 2010-12-03 @ 18:13
Hi Didip,
2010/12/2 Didip Kerabat <didipk@gmail.com>:
> Hi Rajiv,
>
>>
>> Environment: Ubuntu 10.10, Python 2.6.6
>>
>> I am following the tutorial on Step 3: Creating the database.
>> 1) I did not have sqlite3. So I installed using apt-get but outside
>> the virtual environment
>
> sqlite3 is already included as part of Python 2.6, so you shouldn't need
to do anything here.
>
>
>> 2) I then ran sqlite3 /tmp/flaskr.db < schema.sql. No error but no
>> flaskr.db created either.
>
> After you did sqlite3 /tmp/flaskr.db < schema.sql, can you do:
>
> sqlite3 /tmp/flaskr.db
>
> if successful, you should be getting sqlite3 prompt. If not, can you
show what your schema.sql look like?
RAJIV: Sorry, this actually worked for me. I now do have an empty
table in my flaskr.db. I do get the sqlite3 prompt
>
> Also, this step is unnecessary if you wish to do init_db()
>
>> 3) I then copied the code in the tutorial regarding init_db(). I gave
>> my ubuntu usr/pwd. I ran it and I got the following error
>> =============ERROR MESSAGE================
>> (env)vijar82@ubuntu:~/workspace/python/flaskr$ python
>> Python 2.6.6 (r266:84292, Sep 15 2010, 15:52:39)
>> [GCC 4.4.5] on linux2
>> Type "help", "copyright", "credits" or "license" for more information.
>>>>> from flaskr import init_db
>> init>>> init_db()
>
> Is your schema.sql located here? ~/workspace/python/flaskr
>
RAJIV: Yes, my schema.sql is located in ~/workspace/python/flaskr
>> Traceback (most recent call last):
>> File "<stdin>", line 1, in <module>
>> File "flaskr.py", line 26, in init_db
>> db.commit()
>> sqlite3.ProgrammingError: Cannot operate on a closed database.
>> =============ERROR MESSAGE================
>>
>> I then created an empty /tmp/flaskr.db but I got the same error.
>> Here is my flaskr.py.
>> ==================== CODE========================
>> # all the imports
>> from __future__ import with_statement
>> from contextlib import closing
>> import sqlite3
>> from flask import Flask, request, session, g, redirect, url_for, \
>> abort, render_template, flash
>>
>> # configuration
>> DATABASE = '/tmp/flaskr.db'
>> DEBUG = True
>> SECRET_KEY = 'development key'
>
>
>> USERNAME = 'Is this linux or db username'
>> PASSWORD = 'Is this linux or db pwd'
>>
>
> You don't need USERNAME and PASSWORD here.
>
> Also, there's nothing special about all these uppercase words. They are
just variables. That said, you should change your connect_db() to:
>
> def connect_db():
> return sqlite3.connect(DATABASE) # instead of
sqlite3.connect(app.config['DATABASE'])
>
RAJIV: Changed to above code. Still did not work.
>> # create our little application :)
>> app = Flask(__name__)
>> app.config.from_object(__name__)
>>
>> def connect_db():
>> return sqlite3.connect(app.config['DATABASE'])
>>
>> def init_db():
>> with closing(connect_db()) as db:
>> with app.open_resource('schema.sql') as f:
>> db.cursor().executescript(f.read())
>> db.commit()
>>
>>
>>
>>
>> if __name__ == '__main__':
>> app.run()
>
> Hope that helps.
>
> - Didip -
>
>
I dont know if this is relevant but my virtual environment is
~/env/python/flask which is different from where my code exists
~/workspace/python/flaskr
--
Take care,
Rajiv