librelist archives

« back to archive

database problem

database problem

From:
John Pendexter
Date:
2011-05-22 @ 21:26
I am pretty new to web development and python, so forgive me if this is a
trivial question!

I am following the flaskr tutorial and I see to be having an issue with the
database setup.

Here is the code I have so far:

# 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 = '/databases/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)

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

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

def init_db():
    with closing(connect_db()) as db:
        with app.open_resource('schema.sql') as f:
            db.cursor().executescript(f.read())
        db.commit()

@app.before_request
def before_request():
    g.db = connect_db()

@app.after_request
def after_request(response):
    g.db.close() #@UndefinedVariable
    return response



# run the application
if __name__ == '__main__':
    app.run()


and here is the error I am receiving when I try and run init_db()

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "D:/code/flask\flaskr.py", line 35, in init_db
    with closing(connect_db()) as db:
  File "D:/code/flask\flaskr.py", line 29, in connect_db
    return sqlite3.connect(app.config['DATABASE'])
sqlite3.OperationalError: unable to open database file

As far as I can tell it is an issue with the location of the database file?
I have tried numerous combinations of paths to where I set up my folders,
but nothing has worked.

Any help would be appreciated!

Thanks,
John

Re: [flask] database problem

From:
Anthony Ford
Date:
2011-05-24 @ 14:36
I think the problem may be coming from the fact that your DATABASE path is
'/databases/flaskr.db' which on a linux machine would be pointing to
/databases, off of root ( '/' ), not the databases folder in your current
path (where your python file is).

I'm not familiar with windows, so anyone out there more familiar please
correct me if I'm wrong, but I do believe that /databases would map to
C:\databases as well, also not pointing to the databases folder in your
current path. This would also cause problems with your new project pointing
DATABASE to C:\tmp like Chris Aliipule said an email or two back.



Anthony Ford,
KF5IBN,
ford.anthonyj@gmail.com


On Sun, May 22, 2011 at 16:26, John Pendexter <john.pendexter@gmail.com>wrote:

> # configuration
> DATABASE = '/databases/flaskr.db'
> DEBUG = True
>

Re: [flask] database problem

From:
blucalvin
Date:
2011-05-24 @ 17:26
One of my friends currently was doing the microblog tutorial in windows. He
had the database problem. What he did to make it work was to remove the / at
the beginning of "/tmp/flaskr.db".

#configuration
DATABASE = 'tmp/flaskr.db'

Doing like that, a new folder will be created in the directory from where
you invoked the flaskr.py script and everything else seemed to work fine.

On 24 May 2011 20:06, Anthony Ford <ford.anthonyj@gmail.com> wrote:

> I think the problem may be coming from the fact that your DATABASE path is
> '/databases/flaskr.db' which on a linux machine would be pointing to
> /databases, off of root ( '/' ), not the databases folder in your current
> path (where your python file is).
>
> I'm not familiar with windows, so anyone out there more familiar please
> correct me if I'm wrong, but I do believe that /databases would map to
> C:\databases as well, also not pointing to the databases folder in your
> current path. This would also cause problems with your new project pointing
> DATABASE to C:\tmp like Chris Aliipule said an email or two back.
>
>
>
> Anthony Ford,
> KF5IBN,
> ford.anthonyj@gmail.com
>
>
> On Sun, May 22, 2011 at 16:26, John Pendexter <john.pendexter@gmail.com>wrote:
>
>> # configuration
>> DATABASE = '/databases/flaskr.db'
>> DEBUG = True
>>
>
>

Re: [flask] database problem

From:
Owen Marshall
Date:
2011-05-24 @ 17:31
On 05/24/2011 01:26 PM, blucalvin wrote:
> One of my friends currently was doing the microblog tutorial in windows.
> He had the database problem. What he did to make it work was to remove
> the / at the beginning of "/tmp/flaskr.db".
> 
> #configuration
> DATABASE = 'tmp/flaskr.db'
> 
> Doing like that, a new folder will be created in the directory from
> where you invoked the flaskr.py script and everything else seemed to
> work fine.
> 

In general, when debugging file path problems, the easiest thing to test
is `open('rw', filepath)`.

When it doesn't work, you can figure out how to represent paths on your
OS ;-)

-- 
Owen Marshall
FacilityONE
http://www.facilityone.com | (502) 805-2126

Re: database problem

From:
blucalvin
Date:
2011-05-24 @ 12:58
Are  you working on a linux platform?

Re: [flask] database problem

From:
Ron DuPlain
Date:
2011-05-22 @ 22:05
Hi John,

On Sun, May 22, 2011 at 5:26 PM, John Pendexter
<john.pendexter@gmail.com> wrote:
> and here is the error I am receiving when I try and run init_db()
>
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
>   File "D:/code/flask\flaskr.py", line 35, in init_db
>     with closing(connect_db()) as db:
>   File "D:/code/flask\flaskr.py", line 29, in connect_db
>     return sqlite3.connect(app.config['DATABASE'])
> sqlite3.OperationalError: unable to open database file

It doesn't look like you created the database.
See the flaskr README:
https://github.com/mitsuhiko/flask/blob/master/examples/flaskr/README

    2. fire up a python shell and run this:

        >>> from flaskr import init_db; init_db()

Does that fix the issue?

Ron

Re: [flask] database problem

From:
John Pendexter
Date:
2011-05-22 @ 22:16
The directory does exist.

I am receiving the error message when I try to create the database with this
command in the python shell:

from flaskr import init_db; init_db()

John

On Sun, May 22, 2011 at 6:05 PM, Ron DuPlain <ron.duplain@gmail.com> wrote:

> Hi John,
>
> On Sun, May 22, 2011 at 5:26 PM, John Pendexter
> <john.pendexter@gmail.com> wrote:
> > and here is the error I am receiving when I try and run init_db()
> >
> > Traceback (most recent call last):
> >   File "<stdin>", line 1, in <module>
> >   File "D:/code/flask\flaskr.py", line 35, in init_db
> >     with closing(connect_db()) as db:
> >   File "D:/code/flask\flaskr.py", line 29, in connect_db
> >     return sqlite3.connect(app.config['DATABASE'])
> > sqlite3.OperationalError: unable to open database file
>
> It doesn't look like you created the database.
> See the flaskr README:
> https://github.com/mitsuhiko/flask/blob/master/examples/flaskr/README
>
>    2. fire up a python shell and run this:
>
>        >>> from flaskr import init_db; init_db()
>
> Does that fix the issue?
>
> Ron
>

Re: [flask] database problem

From:
John Pendexter
Date:
2011-05-24 @ 02:00
So I went ahead and created a whole new project and copied the code directly
from the flaskr example on git, rather than typing it myself while following
the tutorial in case I was making a typing error somewhere.

I created the /tmp/ folder in the same directory as the flaskr.py file. I
ran the command:

from flaskr import init_db; init_db()

and I received the same error as before.

any thoughts? I know this is going to end up being something really stupid.

Thanks,
John

On Sun, May 22, 2011 at 6:16 PM, John Pendexter <john.pendexter@gmail.com>wrote:

> The directory does exist.
>
> I am receiving the error message when I try to create the database with
> this command in the python shell:
>
>
> from flaskr import init_db; init_db()
>
> John
>
>
> On Sun, May 22, 2011 at 6:05 PM, Ron DuPlain <ron.duplain@gmail.com>wrote:
>
>> Hi John,
>>
>> On Sun, May 22, 2011 at 5:26 PM, John Pendexter
>> <john.pendexter@gmail.com> wrote:
>> > and here is the error I am receiving when I try and run init_db()
>> >
>> > Traceback (most recent call last):
>> >   File "<stdin>", line 1, in <module>
>> >   File "D:/code/flask\flaskr.py", line 35, in init_db
>> >     with closing(connect_db()) as db:
>> >   File "D:/code/flask\flaskr.py", line 29, in connect_db
>> >     return sqlite3.connect(app.config['DATABASE'])
>> > sqlite3.OperationalError: unable to open database file
>>
>> It doesn't look like you created the database.
>> See the flaskr README:
>> https://github.com/mitsuhiko/flask/blob/master/examples/flaskr/README
>>
>>    2. fire up a python shell and run this:
>>
>>        >>> from flaskr import init_db; init_db()
>>
>> Does that fix the issue?
>>
>> Ron
>>
>
>

Re: [flask] database problem

From:
Chris Aliipule
Date:
2011-05-24 @ 02:05
If you're on a windows machine /tmp/ would refer to C:\tmp.

On Mon, May 23, 2011 at 4:00 PM, John Pendexter <john.pendexter@gmail.com>wrote:

> So I went ahead and created a whole new project and copied the code
> directly from the flaskr example on git, rather than typing it myself while
> following the tutorial in case I was making a typing error somewhere.
>
> I created the /tmp/ folder in the same directory as the flaskr.py file. I
> ran the command:
>
>
> from flaskr import init_db; init_db()
>
> and I received the same error as before.
>
> any thoughts? I know this is going to end up being something really stupid.
>
>
> Thanks,
> John
>
>
> On Sun, May 22, 2011 at 6:16 PM, John Pendexter <john.pendexter@gmail.com>wrote:
>
>> The directory does exist.
>>
>> I am receiving the error message when I try to create the database with
>> this command in the python shell:
>>
>>
>> from flaskr import init_db; init_db()
>>
>> John
>>
>>
>> On Sun, May 22, 2011 at 6:05 PM, Ron DuPlain <ron.duplain@gmail.com>wrote:
>>
>>> Hi John,
>>>
>>> On Sun, May 22, 2011 at 5:26 PM, John Pendexter
>>> <john.pendexter@gmail.com> wrote:
>>> > and here is the error I am receiving when I try and run init_db()
>>> >
>>> > Traceback (most recent call last):
>>> >   File "<stdin>", line 1, in <module>
>>> >   File "D:/code/flask\flaskr.py", line 35, in init_db
>>> >     with closing(connect_db()) as db:
>>> >   File "D:/code/flask\flaskr.py", line 29, in connect_db
>>> >     return sqlite3.connect(app.config['DATABASE'])
>>> > sqlite3.OperationalError: unable to open database file
>>>
>>> It doesn't look like you created the database.
>>> See the flaskr README:
>>> https://github.com/mitsuhiko/flask/blob/master/examples/flaskr/README
>>>
>>>    2. fire up a python shell and run this:
>>>
>>>        >>> from flaskr import init_db; init_db()
>>>
>>> Does that fix the issue?
>>>
>>> Ron
>>>
>>
>>
>

Re: [flask] database problem

From:
Drew Vogel
Date:
2011-05-22 @ 22:02
Does the databases dir exist? The sqlite driver will create the file if it
doesn't exist, but I don't think it will create the directories on the path
to the file.
On May 22, 2011 4:27 PM, "John Pendexter" <john.pendexter@gmail.com> wrote:
> I am pretty new to web development and python, so forgive me if this is a
> trivial question!
>
> I am following the flaskr tutorial and I see to be having an issue with
the
> database setup.
>
> Here is the code I have so far:
>
> # 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 = '/databases/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)
>
> def connect_db():
> return sqlite3.connect(app.config['DATABASE'])
>
> if __name__ == '__main__':
> app.run()
>
> def init_db():
> with closing(connect_db()) as db:
> with app.open_resource('schema.sql') as f:
> db.cursor().executescript(f.read())
> db.commit()
>
> @app.before_request
> def before_request():
> g.db = connect_db()
>
> @app.after_request
> def after_request(response):
> g.db.close() #@UndefinedVariable
> return response
>
>
>
> # run the application
> if __name__ == '__main__':
> app.run()
>
>
> and here is the error I am receiving when I try and run init_db()
>
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> File "D:/code/flask\flaskr.py", line 35, in init_db
> with closing(connect_db()) as db:
> File "D:/code/flask\flaskr.py", line 29, in connect_db
> return sqlite3.connect(app.config['DATABASE'])
> sqlite3.OperationalError: unable to open database file
>
> As far as I can tell it is an issue with the location of the database
file?
> I have tried numerous combinations of paths to where I set up my folders,
> but nothing has worked.
>
> Any help would be appreciated!
>
> Thanks,
> John

Wouldnt display my text with breaks

From:
Bijur
Date:
2011-05-22 @ 22:46
Dear folks,

Thanks for this list.

I have a small app which needs to show in a table a few fields of text. 
But no matter how I try, on the final display the text would ignore all 
linebreaks, paragraph breaks and any sort of intendation that is in the 
database. 

Any help would be appreciated. Its 1 am and I am tired! :(

Cheers, 
Abdul

Re: [flask] Wouldnt display my text with breaks

From:
Wilson Xu
Date:
2011-05-23 @ 02:26
Hey Bijur,

Could you pls post more info? Such as data in database, and html codes.

I've run into such problem before.

My data in database:
line1\nline2\nline3

My html codes:
<p>{{ data }}</p>

Then I changed <p> to <pre>, fixed.

My thinking path was:
First, does the data stored in database was right?
If yes, then does the data was changed by logic codes?
If no, then check out the html codes.

Good luck.


On Mon, May 23, 2011 at 6:46 AM, Bijur <bijur@grep-i.com> wrote:

> Dear folks,
>
> Thanks for this list.
>
> I have a small app which needs to show in a table a few fields of text. But
> no matter how I try, on the final display the text would ignore all
> linebreaks, paragraph breaks and any sort of intendation that is in the
> database.
>
> Any help would be appreciated. Its 1 am and I am tired! :(
>
> Cheers,
> Abdul
>



-- 
Think and code - imwilsonxu.net

Re: [flask] Wouldnt display my text with breaks

From:
Adam Oakman
Date:
2011-05-23 @ 00:23
I assume that html is stored in the database, if so are you using the  
safe filter when outputting it? 
http://jinja.pocoo.org/docs/templates/#html-escaping 
  and http://flask.pocoo.org/docs/templating/#standard-filters

If not are you using a pre tag to display the text?

Thanks

Adam

On May 22, 2011, at 5:46 PM, Bijur wrote:

> Dear folks,
>
> Thanks for this list.
>
> I have a small app which needs to show in a table a few fields of  
> text. But no matter how I try, on the final display the text would  
> ignore all linebreaks, paragraph breaks and any sort of intendation  
> that is in the database.
>
> Any help would be appreciated. Its 1 am and I am tired! :(
>
> Cheers,
> Abdul