I have created a app for crud application.. in my root directory I have a folder called data which contains a db file having the data, Im using sqlite3 it works well in local machine, but it raises an error, Operational Error, can't open a database file,

:rotating_light: Before clicking “Create Topic”, please make sure your post includes the following information (otherwise, the post will be locked). :rotating_light:

  1. Share the link to the public deployed app.
  2. Share the link to your app’s public GitHub repository (including a requirements file).
  3. Share the full text of the error message (not a screenshot).
  4. Share the Streamlit and Python version.

Hi @Nagarjun . The following repo will helps you to solve your issue. I mean the what should be there in GitHub repo.
GitHub Repo

Have u placed the db file in the proper directory? I mean both the app.py and db file should be in the same directory and also it’s important to commit and close db operations.

First of all I use context manger in my function when accessing db connection using with keyword.

So I think not problem in connection.

Is it necessary having a db file in same directory as my main entry of my app… means app.py?

Not always but it depends how your giving the path. If you are giving the right path then there will be no issues. Once check how are giving the path from app.py !

I use utils.py where I can declare all my methods. Utils.py located in a folder called src. That is a folder placed in root directory of app where we have a app.py

Now in utils.py I have a method called load_data()
def load_data() → pd.DataFrame:
These function query the database and return all records as a DataFrame
returns pd.DataFrame(all_records)…
root_dir = os.path.dirname(os.path.dirname(os.path.abspath(file)))
db_path = os.path.join(root_dir, “data”, “madurai.db”)
with sqlite3.connect(database=db_path) as con :
statement = “SELECT * FROM donation_records;”
df = pd.read_sql_query(statement,con, index_col=“id”)
return df

Is this logic is correct?

Hey @Nagarjun , the logic is correct but how you are calling this in your app.py file?
Also instead of using the os module you can directly use
sqlite3.connect(‘madurai.db’) because you are in the src folder and you can easily call this as
from src.utils import function_name in app.py file

The error message amounts to saying that db_path is not correct. Display os.path.abspath(db_path) to check.

Yeah I fix the issue, thankyou for your instant reply​:heart_eyes_cat::heavy_heart_exclamation: