SQLite Table Gets Erased After Reboot

Hello, I’m having issues with my Streamlit app. I have created a SQLite3 connection like this:

import sqlite3
import streamlit as st

@st.cache_resource
def get_db_connection():
    return sqlite3.connect("database.db", check_same_thread=False, uri=True)

conn = get_db_connection()
c = conn.cursor()

c.execute('''
    CREATE TABLE IF NOT EXISTS users (
        user_id TEXT PRIMARY KEY NOT NULL UNIQUE,
        name TEXT NOT NULL,
        email TEXT UNIQUE NOT NULL,
        password TEXT NOT NULL,
    )''')
conn.commit()

I can use the database in the Streamlit app with no any issues. However, when the app reboots, the DB gets erased along with all the table structures and data inside. The whole database.db file gets removed. I need to create it again, but it causes irreversible data loss. How can I find a solution to this?

Thanks.

If you’re running locally, this should work fine.

If you’re running this on Community Cloud, then unfortunately any files that you create (including .db files) will not persist across app reboots. To resolve this, the best way is to use a persistent 3rd-party database, such as the ones from here Connect to data sources - Streamlit Docs.