Forums

sqlite3.OperationalError: unable to open database file

Hello there.

I'm a full beginner at Python. I have a small script that successfully runs on my own PC but when I try to run in on pythonanywhere.com I have these errors:

P.S. I already installed the required pip module pyrogram. Also don't pay attention at the first line it isn't necessary: "TgCrypto is missing! Pyrogram will work the same, but at a much slower speed. More info"

TgCrypto is missing! Pyrogram will work the same, but at a much slower speed. More info: https://docs.pyrogram.org/topics/tgcrypto
Traceback (most recent call last):
  File "/home/SomeUsernameXD/forward.py", line 22, in <module>
    app.run()
  File "/home/SomeUsernameXD/.local/lib/python3.9/site-packages/pyrogram/methods/utilities/run.py", line 61, in run
    self.start()
  File "/home/SomeUsernameXD/.local/lib/python3.9/site-packages/pyrogram/sync.py", line 56, in async_to_sync_wrap
    return loop.run_until_complete(coroutine)
  File "/usr/local/lib/python3.9/asyncio/base_events.py", line 642, in run_until_complete
    return future.result()
  File "/home/SomeUsernameXD/.local/lib/python3.9/site-packages/pyrogram/methods/utilities/start.py", line 53, in start
    is_authorized = await self.connect()
  File "/home/SomeUsernameXD/.local/lib/python3.9/site-packages/pyrogram/methods/auth/connect.py", line 39, in connect
    await self.load_session()
  File "/home/SomeUsernameXD/.local/lib/python3.9/site-packages/pyrogram/client.py", line 669, in load_session
    await self.storage.open()
  File "/home/SomeUsernameXD/.local/lib/python3.9/site-packages/pyrogram/storage/file_storage.py", line 107, in open
    self.conn = sqlite3.connect(str(path), timeout=1, check_same_thread=False)
sqlite3.OperationalError: unable to open database file```

Screenshot

[Edit by admin: formatting]

Make sure that the path to the db file is set correctly (it's probably different that on your local machine).

exactly the same problem

What code are you using to load up the database? Where is the database file stored?

the problem is i dont use any database. it just the error from the pyrogram library and idk what to do with it

What is the error message that you are getting?

self.conn = sqlite3.connect(str(path), timeout=1, check_same_thread=False) ==>

self.conn = sqlite3.connect(f"/home/user/.local/{path}", timeout=1, check_same_thread=False)

Could you provide a snippet of the code that is generating the error?

same error. when run through the bash command line, everything works fine on your service, the full path is registered using pathlib. But I just can’t run this code through Tasks, it gives a bunch of errors and everything is connected in files (.db, .py) that are in other packages/folders. https://github.com/ArtDmSav/TG_estate_bot_CY

@artdmsav Could you point to the palace in that repo that is causing errors?

@fjl When i create and start Always-on tasks : python3.10 /home/artdmsav/estate_bot/bot_aiogram.py

I have this: 2023-08

-21 10:43:29 - Task preparing to start
Aug 21 10:44:20 Traceback (most recent call last):
Aug 21 10:44:20   File "/home/artdmsav/estate_bot/bot_aiogram.py", line 27, in <module>
Aug 21 10:44:20     token = config['Telegram']['bot_token']
Aug 21 10:44:20   File "/usr/local/lib/python3.10/configparser.py", line 964, in __getitem__
Aug 21 10:44:20     raise KeyError(key)
Aug 21 10:44:20 KeyError: 'Telegram'

and this good work with bash console

You're probably reading from a config file using a relative path without paying attention to the working directory: https://help.pythonanywhere.com/pages/NoSuchFileOrDirectory/

1

I sent 3 identical messages and I can't delete them

@fjl I am using absolute path. Correct me if I'm wrong.

import configparser
from pathlib import Path

dir_path = Path.cwd() 
path = Path(dir_path, 'config', 'config.ini')

config = configparser.ConfigParser() 
config.read(path)

token = config['Telegram']['bot_token']

@fjl I changed it as indicated in the example on the link and it helped, although before that I also used the function for the obscure path, but it didn’t work with it. Thank you

The code that you posted above would not be an absolute path -- that, I think, is what was causing the issue. The cwd function returns the process's current working directory, so putting it at the start of a path is equivalent to using a relative path. If you want to construct a path that is relative to the file where the code appears, you'd need to use this:

dir_path = Path(os.path.dirname(__file__))