How to save to DB after a button click

I am going to save the settings of widgets into database when a button is clicked. Here is a part of the code:

title = st.text_inpur(“Title:”)
category = st.selectbox(…)
… # other data widgets
if st.button(“Save”):
db = mysql.connector.connect(…)
cur = db.cursor()
sql = … # make a INSERT SQL statement from the widgets input, pls neglect issues of sql injection
cur.execute(sql)
db.commit()

I expect it to save data in MySQL after “save” button is clicked. But what happens is, after clicking the button, the corresponding webpage is blocked and showing “Running” all the time, and the database is not changed.

Any advice? Thanks all.

hi team, could you please give me some advice? thanks in advance.

Hi @cuteufo,

There can be many reasons for why its saying Running, I would start debugging this issue by checking if I am able to connect and run the insert query from a python script from the system which is running streamlit. Then checking the size of data that you are trying to insert whether its too large.
Also do you see any error ? If so please try posting it here.
I have been able to insert the data in databases from streamlit successfully in some of my apps so I think the problem must be with the way you are inserting the data or connection to db. But at this point I am just speculating. :slight_smile:

I hope it helps you to debug in the right direction!