I am trying to update a table through streamlit. I am implementing this locally.
Below is the main file:
import streamlit as st
# Initialize connection.
conn = st.connection("postgresql", type="sql")
person = 124
new = 12345558
try:
conn.query(f"UPDATE user_table SET password = '{new}' WHERE person='{person}'")
conn.commit()
except Exception as e:
print(e)
finally:
st.write("Yessss!!")
Unfortunately, this action did not provide the necessary assistance. The connection is closing before the query executes, resulting in the values not being updated in the database.
Hi @Explorer . Remove the finally block and in the except block use st.write(e) instead of print(e). Also, add st.write(‘yes’) after conn.close(). Once try this
st.connection is a means to fetch data as easily, as possible. It is not meant currently to run arbitrary queries, at least that’s my understanding from the docs on streamlit site.
If you want to run arbitrary queries - either use low-level library, like psycopg2 (psycopg2-binary) for Postgres, or a higher-level ORM, like sqlalchemy.
SQLConnection provides the query() convenience method, which can be used to run simple read-only queries with both caching and simple error handling/retries. More complex DB interactions can be performed by using the .session property to receive a regular SQLAlchemy Session.