Uploading and using database with file_uploader

Hello everyone,
I’ve been learning various CS topics for few years now and Streamlit has been really helpful to me. Lately I’ve been trying to learn more about databases in general. Here’s the issue I’ve encountered:

I’d like for users to be able to load their own sqlite database with file_uploader. Once it’s loaded, the app would connect to it, fetch some data, adjust it with pandas and show it to the user. The user could then change it with data_editor, insert it into the database and download it.

The issue is that when I try to pass the file_uploade object into the connection I don’t get anything in return. The database I’m uploading works normally when I coonnect to it locally.
Is what I’m trying to do doable without saving the database to a tmp folder? Is it considered bad practice?

Here’s the code:

import streamlit as st
import sqlite3

user_db = st.file_uploader("Upload your database", type=".sqlite3")

if user_db is not None:
    connection = sqlite3.connect(user_db.name)
    cursor = connection.cursor()

    cursor.execute("SELECT * FROM sqlite_master;")
    data = cursor.fetchall()

    st.write(data)
    connection.close()

image

Hi @b_adam,

I think you will have to either:
a. save the uploaded sqllite db on your server and then create a connection to it as you would normally do with a local db file OR
b. as the uploaded file is in memory, you may want to experiment with the in-memory functions of sqllite ( In-Memory Databases (sqlite.org))

Cheers

I see, thank you.
Could you enlighten me why I’m getting no data in my former solution?