How do I connect my python app to sqlite?

8 months ago

I enabled private networking, not really sure whats next. Tried giving the private network url as sqlite url, but thats erroring out.

SQLALCHEMYDATABASEURL = "sqlite3.railway.internal:////app/data/database.db"

0 Replies

8 months ago

7418d6aa-d7a2-45b1-962f-bfd0f0773a06


8 months ago

sqlite is a file on disk, I'm not sure how the private network comes into play here.

you need to store the sqlite file in a volume, or use Postgres.


8 months ago

I set it up from a template, with volume it wasnt working either

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


8 months ago

# Use an official Python runtime as the base image
FROM python:3.9-slim

# Set the working directory in the container
WORKDIR /app

# Copy the requirements file into the container
COPY requirements.txt .

# Install the required packages
RUN pip install --no-cache-dir -r requirements.txt

# Copy the rest of the application code
COPY . .

# Create a non-root user and switch to it
RUN adduser --disabled-password --gecos '' appuser

# Create a directory for the database and set permissions
RUN mkdir -p /app/data && chown -R appuser:appuser /app/data

# Switch to the non-root user
USER appuser

# Make sure the static directory exists
RUN mkdir -p static

# Expose the port the app runs on
EXPOSE 8000

# Set the database path to the persisted volume
ENV DATABASE_URL=sqlite:////app/data/database.db

# Command to run the application
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]

8 months ago

dockerfile


8 months ago

# Database setup
SQLALCHEMY_DATABASE_URL = "sqlite:////app/data/database.db"

8 months ago

created a volume at /app/data


8 months ago

please enclose that all in triple backticks


8 months ago

the time it was working, it wasnt persisting on redeploys 😔


8 months ago

So Im confused now


8 months ago

that means you are not using the correct paths to save the db file


8 months ago

volume mount path is - /app/data


8 months ago

what should be sqlite uri?


8 months ago

/app/data/database.db


8 months ago

thats not working


8 months ago

same error sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) unable to open database file


8 months ago

are you sure your code is setup to create a database file if one doesn't exist, since this is an empty volume, a database file indeed won't exist


8 months ago

# Database setup
SQLALCHEMY_DATABASE_URL="sqlite:////app/data/database.db"
engine = create_engine(SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False})
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
Base = declarative_base()

8 months ago

I don't know if any of that is valid syntax, you would need to reference SQL alchemy's docs for that


8 months ago

it is valid, works locally.
anyways, i will try figure out on my own


8 months ago

its just not happening man what the hell


8 months ago

😭


8 months ago

imma switch over to a normal vps at this point


8 months ago

ok it worked now 😭


8 months ago

what did you do?


How do I connect my python app to sqlite? - Railway Help Station