Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stuck at Flask tutorial step 3

Following Flask tutorial, running Win 7, Python 2.7.3, virtualenv, and I am stuck in Step 3: Creating The Database http://flask.pocoo.org/docs/tutorial/dbinit/#tutorial-dbinit

Such a schema can be created by piping the schema.sql file into the sqlite3 command as follows:

sqlite3 /tmp/flaskr.db < schema.sql

How to run this command, because CMD < venv > returns:

"sqlite3" is not recognized as internal or external command, operable program or batch file.

Is this step necessary?

Folder Project, 2 files schema.sql and flaskr.py.

schema.sql

drop table if exists entries;
create table entries (
  id integer primary key autoincrement,
  title string not null,
  text string not null
);

flaskr.py

# all the imports
import sqlite3
from flask import Flask, request, session, g, redirect, url_for, \
abort, render_template, flash
from contextlib import closing
# configuration
DATABASE = '/tmp/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'])

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

< venv > python

>>> from flaskr import init_db
>>> init_db()
Trackeback <most recent call last>:
File "<stdin>", line 1, in <module>
File "flaskr.py", line 24, in init_db
  with closing (connect_db()) as db: 
File "flaskr.py", line 21, in connect_db
return sqlite3.connect(app.config['DATABASE'])
sqlite3.OperationalError: unable to open database.
like image 587
That_User Avatar asked Apr 01 '13 09:04

That_User


4 Answers

You are confused between Windows and UNIX filesystems.

Find out where sqllite.exe file exists on the computer. lets say it is in C:\sqllite. Then you also need to determine where you will create the database file. /tmp/flaskr.db is for the UNIX filesystem. On windows, you should provide the exact path or in your current working directory. lets say it is C:\flasktutorial.

To be safe, you might want to create a blank flaskr.db file first.

Open a notepad and create the blank file at `C:\flasktutorial\flaskr.db`

Now you can run:

C:\sqllite\sqllite.exe C:\flasktutorial\flaskr.db < schema.sql

Also make sure that in your flaskr.py file, change the DATABASE to:

DATABASE = 'C:\flasktutorial\flaskr.db'
like image 127
codegeek Avatar answered Oct 20 '22 11:10

codegeek


Did you activate virtualenv and installed flask?

flask should have sqlite3 by default. I got following error though:

 File "flaskr.py", line 26, in connect_db
  return sqlite3.connect(app.config['DATABASE'])
sqlite3.OperationalError: unable to open database file`

To fix that I had to do the following (in Windows):

  1. Change DATABASE = '/tmp/flaskr.db' to DATABASE = '.\\tmp\\flaskr.db'
  2. Create tmp folder in current folder (flaskr)
  3. Create an empty flaskr.db file in tmp

After that it's working for me.

like image 43
K246 Avatar answered Oct 20 '22 10:10

K246


As you can observe from the error logs, the error is coming while connecting DB

sqlite3.OperationalError: unable to open database.

And since this step failed for you,

sqlite3 /tmp/flaskr.db < schema.sql

You can easily interpret that this step is necessary. Now to solve this error, simply you have to install sqlite3, In ubuntu you can install sqlite3 as,

apt-get install sqlite3

After installation your program will work fine as expected.

like image 1
a.m. Avatar answered Oct 20 '22 11:10

a.m.


I'm not sure if these tips are directly applicable to the PO, but they landed me here, so maybe it will be helpful.

Be sure to check the README file within the flaskr directory which specifies running flask --app=flaskr initdb from the command line.

If this returns AttributeError: 'Flask' object has no attribute 'cli', it may be due to click not being installed.

If the command returns flask: command not found, it may be due to having an older version of Flash installed, which is what pip install flask does.

As of today this command pip install https://github.com/mitsuhiko/flask/tarball/master, will install the most recent version.

like image 1
MikeiLL Avatar answered Oct 20 '22 09:10

MikeiLL