R
Railwayβ€’3mo ago
Rithik

How do I connect my python app to sqlite?

I enabled private networking, not really sure whats next. Tried giving the private network url as sqlite url, but thats erroring out. SQLALCHEMY_DATABASE_URL = "sqlite3.railway.internal:////app/data/database.db"
15 Replies
Percy
Percyβ€’3mo ago
Project ID: 7418d6aa-d7a2-45b1-962f-bfd0f0773a06
Rithik
RithikOPβ€’3mo ago
7418d6aa-d7a2-45b1-962f-bfd0f0773a06
Brody
Brodyβ€’3mo 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. https://docs.railway.app/guides/volumes
Rithik
RithikOPβ€’3mo ago
I set it up from a template, with volume it wasnt working either sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) unable to open database file
# 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"]
# 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"]
dockerfile
# Database setup
SQLALCHEMY_DATABASE_URL = "sqlite:////app/data/database.db"
# Database setup
SQLALCHEMY_DATABASE_URL = "sqlite:////app/data/database.db"
created a volume at /app/data
Brody
Brodyβ€’3mo ago
please enclose that all in triple backticks
Rithik
RithikOPβ€’3mo ago
the time it was working, it wasnt persisting on redeploys πŸ˜” So Im confused now
Brody
Brodyβ€’3mo ago
that means you are not using the correct paths to save the db file
Rithik
RithikOPβ€’3mo ago
volume mount path is - /app/data what should be sqlite uri?
Brody
Brodyβ€’3mo ago
/app/data/database.db
Rithik
RithikOPβ€’3mo ago
thats not working same error sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) unable to open database file
Brody
Brodyβ€’3mo 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
Rithik
RithikOPβ€’3mo 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()
# 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()
Brody
Brodyβ€’3mo ago
I don't know if any of that is valid syntax, you would need to reference SQL alchemy's docs for that
Rithik
RithikOPβ€’3mo ago
it is valid, works locally. anyways, i will try figure out on my own its just not happening man what the hell 😭 imma switch over to a normal vps at this point ok it worked now 😭
Brody
Brodyβ€’3mo ago
what did you do?
Want results from more Discord servers?
Add your server