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.
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.