#🔒 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
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.
can you show the line in which you try to connect to the database ?
and send a picture of your file structure if possible
def connect_db():
"""
Connect to the local SQLite database (movies-1.db).
Returns:
sqlite3.Connection: Database connection object if successful.
None: If connection fails.
"""
Hey @acoustic iron!
You are using the wrong character instead of backticks. Use ```, not """.
wheres the actual code that connects to the file
ai generated 🥀
it's lovely to have docstring, but we are talking about the code ^^
seems like it XD
docstring but no tyoehints?
thats what i use to connect to the .db
forgive me if it isnt perfect im a beginner 😔
can you take a screenshot of the file structure ?
how do you run the python script ?
I press f5 to run the script
yes
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
could you elaborate?
do you know what is the try/except doing ?
How do you know the connection is not working ?
ill get an error on my app saying database connection error
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
Just replace except sqlite3.Error: return None with except Exception: raise (even tho thats not good practice, but its for debugging)
Put:
import os
print("cwd =", os.getcwd())
in the connect_db() function just above thr try:.
The usual reason is that you're not in the folder you think you are and therefore a relative filename like "movies-1.db" is wrong.
@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?
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
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.)
@acoustic iron
Thank you, ive been stuck on this for way to long and you just saved me
🙏
yep
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.