matt hoover
matt hoover

Reputation: 346

OperationalError: unable to open database file in PyCharm with Flask IDE plug-in

I have been struggling with this for the better part of the day. I am absolutely incapable of connecting to my database in PyCharm. I even follow the video tutorial exactly while downloading the tutorial source from github. PLEASE HELP!

http://www.youtube.com/watch?v=jaFEODzG7B8 https://github.com/mitsuhiko/flask/tree/master/examples/flaskr/

# 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 = "flaskr.db"
DEBUG = True
SECRET_KEY = 'development key'
USERNAME = 'admin'
PASSWORD = 'default'

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

I get the following error when I try to initialize the database:

>>> init_db()
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "C:\Users\Intern2\PycharmProjects\flaskr\flaskr.py", line 23, in init_db
    with closing(connect_db()) as db:
  File "C:\Users\Intern2\PycharmProjects\flaskr\flaskr.py", line 20, in connect_db
return sqlite3.connect(app.config['DATABASE'])
OperationalError: unable to open database file

Literally any theories would be greatly appreciated!

UPDATE: I created the database file, but now I cannot for the life of me locate it. I have tried multiple versions of paths.

DATABASE = '/Users/snuffles753/PycharmProjects/flaskr/flaskr.db'
DATABASE = 'flaskr.db

etc... the file is located in the exact same place as my .py file

Also, I'm using Mac OS

Upvotes: 1

Views: 3635

Answers (1)

codegeek
codegeek

Reputation: 33309

The problem is that you need to have the "flaskr.db" file created BEFORE you can connect to it. It is not created by itself. So do the following:

  1. GO to the directory where you flask.py code is there. Run the following:

    touch flaskr.db

  2. Make sure permission are allowed on this file:

    chmod 777 flaskr.db

Upvotes: 2

Related Questions