Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sqlalchemy.exc.OperationalError: (OperationalError) unable to open database file None None

Tags:

I am running a program from another person who are inconvenience ask for help from. The program is a website. Server end is written by python and flask (module, http://flask.pocoo.org/). The program has been successfully run on the server. What I need to do is modify something on it. Since the production server is not allowed for test, I tested it in development server locally via flask. However, I could not run even the original program. Below is from python.

(venv)kevin@ubuntu:~/python/public_html$ python index.wsgi 

Traceback (most recent call last): File "index.wsgi", line 6, in from app import app as application

File "/home/kevin/python/public_html/app.py", line 27, in <module>
app = create_app()

File "/home/kevin/python/public_html/app.py", line 12, in create_app
database.init_db()

File "/home/kevin/python/public_html/database.py", line 24, in init_db
Base.metadata.create_all(engine)

File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/schema.py", line 2793, in create_all
  tables=tables)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 1478, in _run_visitor
with self._optional_conn_ctx_manager(connection) as conn:

File "/usr/lib/python2.7/contextlib.py", line 17, in __enter__
return self.gen.next()

File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 1471, in _optional_conn_ctx_manager
with self.contextual_connect() as conn:

File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 1661, in contextual_connect
self.pool.connect(),

File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/pool.py", line 272, in connect
return _ConnectionFairy(self).checkout()

File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/pool.py", line 425, in __init__
rec = self._connection_record = pool._do_get()

File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/pool.py", line 857, in _do_get
return self._create_connection()

File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/pool.py", line 225, in _create_connection
return _ConnectionRecord(self)

File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/pool.py", line 318, in __init__
self.connection = self.__connect()

File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/pool.py", line 368, in __connect
connection = self.__pool._creator()

File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/strategies.py", line 80, in connect
return dialect.connect(*cargs, **cparams)

File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/default.py", line 283, in connect
return self.dbapi.connect(*cargs, **cparams)

sqlalchemy.exc.OperationalError: (OperationalError) unable to open database file None None

In the config.py file

LOGFILE = '/tmp/ate.log' DEBUG = True TESTING = True THREADED = True DATABASE_URI = 'sqlite:////tmp/ate.db' SECRET_KEY = os.urandom(24)

Hence, I created a folder called "tmp" under my user and an empty file called "ate.db". Then, ran it again. It said

IOError: [Errno 2] No such file or directory: '/home/kevin/log/ate.log'

Then, I created the log folder and the log file. Run it, but nothing happened like

(venv)kevin@ubuntu:~/python/public_html$ python index.wsgi (venv)kevin@ubuntu:~/python/public_html$ python index.wsgi (venv)kevin@ubuntu:~/python/public_html$

If it is successful, the website should be available on http://127.0.0.1:5000/. However, it did not work. Does anybody know why and how to solve it? The codes should be fine since it is now available online. The problem should be a local problem. Thank you so much for your help.

The code of where the program is stuck

from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import scoped_session, sessionmaker

engine = None
db_session = None
Base = declarative_base()


def init_engine(uri, **kwards):
    global engine
    engine = create_engine(uri, **kwards)
    return engine


def init_db():
    global db_session
    db_session = scoped_session(sessionmaker(bind=engine))
    # import all modules here that might define models so that
    # they will be registered properly on the metadata.  Otherwise
    # you will have to import them first before calling init_db()
    import models
    Base.metadata.create_all(engine)
like image 810
user2677756 Avatar asked Aug 13 '13 11:08

user2677756


3 Answers

Replace:

app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////dbdir/test.db'

With:

app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///dbdir/test.db'
like image 78
britodfbr Avatar answered Oct 16 '22 11:10

britodfbr


finally figured it out, had help tho

 import os

file_path = os.path.abspath(os.getcwd())+"\database.db"

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///'+file_path
 db = SQLAlchemy(app)
like image 34
Jayson Kaeze Avatar answered Oct 16 '22 11:10

Jayson Kaeze


I had this issue with sqlite. The process trying to open the database file needs to have write access to the directory as it creates temporary/lock files.

The following structure worked for me to allow www-data to use the database.

%> ls -l
drwxrwxr-x  2 fmlheureux www-data     4096 Feb 17 13:24 database-dir

%> ls -l database-dir/
-rw-rw-r-- 1 fmlheureux www-data 40960 Feb 17 13:28 database.sqlite
like image 37
Finch_Powers Avatar answered Oct 16 '22 09:10

Finch_Powers