#🔒 My program can’t find or open the database file even though its in the same folder as the script.

44 messages · Page 1 of 1 (latest)

acoustic iron
#

i keep getting getting the error message "Database connection error. please check (Name of database)".
Its working fine on my device but this error only shows up when i try to run the program on a different device

glossy gobletBOT
#

@acoustic iron

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

hollow forge
#

can you show the line in which you try to connect to the database ?

old scarab
#

and send a picture of your file structure if possible

acoustic iron
#

def connect_db():
"""
Connect to the local SQLite database (movies-1.db).

Returns:
    sqlite3.Connection: Database connection object if successful.
    None: If connection fails.
"""
glossy gobletBOT
#

Hey @acoustic iron!

Please edit your message to use a code block

You are using the wrong character instead of backticks. Use ```, not """.

old scarab
sterile frost
#

ai generated 🥀

hollow forge
#

it's lovely to have docstring, but we are talking about the code ^^

old scarab
thorn basalt
#

docstring but no tyoehints?

acoustic iron
#

thats what i use to connect to the .db

acoustic iron
hollow forge
#

can you take a screenshot of the file structure ?

acoustic iron
hollow forge
#

how do you run the python script ?

acoustic iron
#

I press f5 to run the script

hollow forge
#

what IDE are you using ?

#

(looks like IDLE, is it ?)

acoustic iron
#

yes

hollow forge
#

I would say it is quite weird, can you print the error instead of simply returning None ?

#

IIRC sql.connect should create a db instead of raising an error if the file doesn't exist yet

hollow forge
#

do you know what is the try/except doing ?

#

How do you know the connection is not working ?

acoustic iron
#

ill get an error on my app saying database connection error

hollow forge
#

in your function, you return None if an sql.Error occurs, instead of this, print the error

#

can you show the full error ?

#

The whole stack of error

thorn basalt
#

Just replace except sqlite3.Error: return None with except Exception: raise (even tho thats not good practice, but its for debugging)

sour fable
acoustic iron
#

so ive realised that ive had to open the file from within VS or IDLE and not from file explorer. was that the issue the whole time?

sour fable
#

I don't know.
But if it's your policy that the db file is in the same folder as your .py file, then you need to compute its full path from the path to the .py file.
Inside the connect_db function you can look up th script filename, get the folder from that, append movies-1.db . That way it will work regardless of how you run the script.
Something like:

import os.path
script_folder = os.path.dirname(__file__)
db_path = os.path.join(script_folder, "movies-1.db")
return sqlite3.connect(db_path)
#

!doc os.path

glossy gobletBOT
#

Source code: Lib/genericpath.py, Lib/posixpath.py (for POSIX) and Lib/ntpath.py (for Windows).

This module implements some useful functions on pathnames. To read or write files see open(), and for accessing the filesystem see the os module. The path parameters can be passed as strings, or bytes, or any object implementing the os.PathLike protocol.

Unlike a Unix shell, Python does not do any automatic path expansions. Functions such as expanduser() and expandvars() can be invoked explicitly when an application desires shell-like path expansion. (See also the glob module.)

sour fable
#

@acoustic iron

acoustic iron
#

🙏

sour fable
#

No worries.

#

You know what the current working directory is?

acoustic iron
#

yep

glossy gobletBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.