How to refresh data from database?

I an a newbie to streamlit,I write an example about read data from Mysql to show on the page. But when I add or delete a record at the database backend such as navicat, the data on the page dosen’t change, even I put a button to rerieve data from database. what’s wrong with my code, anyone can help me ?

import streamlit as st

def getStudentData():
    conn = st.connection("mysql",type = "sql")
    print("getStudentData")
    sql = "select id,name,class,lession from student"
    result = conn.query(sql)
    return result

if 'refreshdata' in st.session_state:
    del st.session_state.data
    del st.session_state.refreshdata

if 'data' not in st.session_state:
    df = getStudentData()
    st.session_state['data'] = df

df = st.session_state.data
st.dataframe(df)

if st.button("refreshdata"):
    st.session_state['refreshddata'] = 1

I have the right to speak, but when we first started using it, we encountered the same problem as you, which was that we did not have a deep understanding of the rules. :point_down:Please add ‘ttl=0’ in ‘conn. query (SQL)’`

result = conn.query(sql, ttl=0)

Thanks for your help, but It doesn’t work after add ttl=0 in ‘conn.query’。

It’s because ‘if’ refreshdata ‘in st. session state:’ and ‘st. session state [’ refreshddata ‘]=1’ are not the same key. :joy:

import streamlit as st

def getStudentData():
    conn = st.connection("mysql",type = "sql")
    print("getStudentData")
    sql = "select * from t_student"
    result = conn.query(sql, ttl=0)
    return result

if 'refreshdata' in st.session_state:
    del st.session_state.data
    del st.session_state.refreshdata

if 'data' not in st.session_state:
    df = getStudentData()
    st.session_state['data'] = df

df = st.session_state.data
st.dataframe(df)

if st.button("refreshdata", key='refreshdata_bt'):
    st.session_state['refreshdata'] = 1

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.