I am going through sample codes in the documentation, deploying Streamlit locally.
In Step 3 of “A simple starting point - using a local SQLite database”, the sample code results in error:
ArgumentError: Textual SQL expression ‘CREATE TABLE IF NOT EXIST…’ should be explicitly declared as text(‘CREATE TABLE IF NOT EXIST…’)
Changes needed:
- from sqlalchemy import text
- to wrap all SQL expression statements in text()
import streamlit as st
from sqlalchemy import text
#create the SQL connection to pets_db as specified in your secrets file
conn = st.connection('pets_db', type='sql')
#insert some data with conn session
with conn.session as s:
s.execute(text('CREATE TABLE IF NOT EXISTS pet_owners (person TEXT, pet TEXT);'))
s.execute(text('DELETE FROM pet_owners;'))
pet_owners = {'jerry': 'fish', 'barbara': 'cat', 'alex': 'puppy'}
for k in pet_owners:
s.execute(
text('INSERT INTO pet_owners (person, pet) VALUES (:owner, :pet);'),
params=dict(owner=k, pet=pet_owners[k])
)
s.commit()
#Query and display the data you inserted
pet_owners = conn.query('select * from pet_owners')
st.dataframe(pet_owners)
In “Step 2: Set a database URL in your Streamlit secrets.toml file”, the file content should be:
[connections.pets_db]
url = "sqlite:///./pets.db" <= note the extra ./
if you have created a .streamlit folder in your application folder, and added secrets.toml to the .streamlit folder.
streamlit version 1.40.0
python version 3.11.9
This post can be deleted if the needed update is made to the documentation.