Forums

Cant access my database from pythonanywhere.com. Site is working fine though.

I have a database that for some reason I cannot interact with through consoles. My website is currently working fine, and the data appears to be up to date.

  • When I try to update a db item from my site, it works fine, and the changes are made.

  • When I try to access the db from the same python file the site would use, I get the error found at the bottom of this post. The most relevant bit being:

.

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

I found a stackoverflow question that closely resembles my case here. While no answer was accepted, the most upvoted solution seems to suggest I do not currently have write access to the directory.

I have tried refreshing the site but it was no better.

Any idea how I can regain access to my db from consoles?

Traceback (most recent call last):
  File "/home/ExperimentsWithCode/getmebro/sqlalchemy_update.py", line 9, in <module>
    from sqlalchemy_declarative3 import Base, Users, Accounts, Contacts, History, Records
  File "/home/ExperimentsWithCode/getmebro/sqlalchemy_declarative3.py", line 175, in <module>
    Base.metadata.create_all(engine)
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/schema.py", line 2848, 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 431, in __init__
    rec = self._connection_record = pool._do_get()
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/pool.py", line 867, 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 379, 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

Are you sure that the config of your database location is the same between your web app and your command line environment?

Yes. I am just pressing 'save and run' or 'control r' while having the python file my webapp uses to update the database open. The error occurs immediately and prevents me from calling any functions at that point, as the file did not load/run. Trying to load the functions in line by line at that point errors when trying to import anything from my declarative files as it does with the initial load.

It occurs when trying to import anything from my declarative files which I have not touched for a couple of weeks if not longer. I have been able to access the db through this method since then. I double checked the path to my db against previous git commits and it has not changed.

DB files are there, and if I make an edit on my website the timestamps update.

I have two separate databases composed by two separate declarative files. Both databases cannot be accessed and exist in the same folder. The errors are essentially identical differ in their respective paths.

Then my guess is you're using relative paths which are based on the current working directory of the process. That is likely to be different between the web app and the console. Use absolute paths to refer to your database file.

Hey, sorry for the super delayed reply. Got swept up in somethings. I appreciate your help!

You were correct that I was using a relative path. I still don't really understand why my relative path had been working for months and for seemingly no reason no longer works. However I'm thankful its working now.

Initially my path was:

engine = create_engine('sqlite:///getmebro/database_3.db')
#     - Does not work in console. 
#     - Does work in web-app!

After much trial and error I discovered my attempts at an absolute path were missing an extra '/' before home.

engine = create_engine('sqlite://///home/ExperimentsWithCode/getmebro/database_3.db')
#     - Does work in console!
#     - Does work in web-app!

This worked


I had previously tried the following variations before honing that down.

#
engine = create_engine('sqlite:////getmebro/database_3.db')
#     - Does not work in console, or web-app
#
engine = create_engine('sqlite:////ExperimentsWithCode/getmebro/database_3.db')
#     - Does not work in console, or web-app
#
engine = create_engine('sqlite:////home/ExperimentsWithCode/getmebro/database_3.db')
#      - Does not work in console, or web-app
#
engine = create_engine('sqlite:////files/home/ExperimentsWithCode/getmebro/database_3.db')
#     - Does not work in console, or web-app
#
engine = create_engine('sqlite:////user/ExperimentsWithCode/files/home/ExperimentsWithCode/getmebro/database_3.db')
#     - Does not work in console, or web-app

These all produced the same error as the original problem.

See here. It seems like you should be able to do

create_engine('sqlite:////home/ExperimentsWithCode/getmebro/database_3.db')