DatabaseError SQLite3 in my app

Hi, I have a problem with sqlite3 and streamlit.
I have a data base called guia_telefonica.db, which has records of diferent provinces.

I want to get and display a dataframe using the province name as a filter.

My code:

import pandas as pd
import sqlite3 as sql

db_guia = r"mypath\guia_telefonica.db"

seleccion_provincia = st.selectbox('Seleccionar Provincia',
        ('BUENOS AIRES','CIUDAD DE BUENOS AIRES', 'CATAMARCA', 'CHACO', 'CHUBUT',
            'CORDOBA', 'CORRIENTES', 'ENTRE RIOS',
        'FORMOSA', 'JUJUY', 'LA PAMPA', 'LA RIOJA', 'MENDOZA', 'MISIONES',
        'NEUQUEN', 'RIO NEGRO', 'SALTA', 'SAN JUAN', 'SAN LUIS',
        'SANTA CRUZ', 'SANTA FE', 'SANTIAGO DEL ESTERO',
        'TIERRA DEL FUEGO', 'TUCUMAN'))


def prov_db(provincia):
    conn = sql.connect(db_guia)
    df_provincia = pd.read_sql_query(f"SELECT * FROM [{provincia}]", conn)
    return df_provincia

st.write(prov_db(seleccion_provincia))

I have the next error:
DatabaseError: Execution failed on sql ‘SELECT * FROM [BUENOS AIRES]’: no such table: BUENOS AIRES

I can run this code in jupyter lab, using ANACONDA… but when I wanted to run it in my streamlit application I got the DatabasError

thanks in advance for any advice!

Hi @Joaquin, welcome to the Streamlit community!

Usually errors like this indicate that you have a bad reference to a file, usually due to starting Streamlit from a different folder than where you’re starting JupyterLab. Have you tried to put the exact file location into db_guia?

You might also want to move that variable inside the prov_db function, unless you expect to make that value selectable in the future. Otherwise, you’re referring to a global Python variable from inside of a function, which can sometimes have weird results.

Best,
Randy