Database updates

My App uses MySQL DB on the server where data changes every few minutes at the backend. I use

I am new to streamlit

st.experimental_connection(‘mysql’, type=‘sql’,ttl=300) to connect to the db and the app is used to present the data. Added the ttl = 300 expecting a refresh. However, the data is not refreshed on the app unless I reboot the app. Any idea where I am going wrong?

Thanks in advance

Hi there!

While I’m not so familiar with experimental_connection, I believe the ttl parameter stores the results of your queries in the cache for the specified amount of time. This means that in your case, once queried, the results are stored in the cache for 300 seconds (5 minutes). For any of the same queries you make, results are pulled from the cache instead of querying the database again, which is why you may be seeing the not being refreshed.
I believe setting the ttl parameter to a much lower value can be the solution. I will try an SQL connection and know if it works. Maybe one of the devs can give a specific answer.


1 Like

I actually added the TTL part to see if it refreshes after 5 mins. In the first place, it does not refresh data with or without ttl.

You can set ttl=0 to disable caching of the resource. Also, if you want to refresh data, you can also add st.experimental_rerun, which reruns the script so that data can be updated again.

1 Like

Thanks. Will try

Does not seem to be working.

To be clear, the data won’t automatically be updated even if the cache is expired, unless something triggers the app to rerun. For example, if you add a “refresh” button and hit that after the cache expires, the query should be redone and show the new data. Does that work for you?

1 Like

Thanks. I worked around it. Used st.cache_data.clear()

1 Like

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