I have created a snowflake streamlit app to writeback data into a table, the insert works fine, the search function work fine, but it does not update the data as expected
# Section for searching and updating existing records
st.subheader("Search for Record to Update")
with st.form("search_record"):
search_by = st.radio("Search by:", ('MCC Code', 'MCC_TITLE'))
search_value = st.text_input(f"Enter {search_by}:")
search_button = st.form_submit_button('Search')
# Display and update form if record is found
if search_button:
try:
search_column = '"MCC Code:"' if search_by == 'MCC Code' else '"MCC_TITLE"'
query = f"""
SELECT * FROM IK_DEV.POWER_WRITE_BACK.MERCHANT_CATEGORY_CODE_LIST
WHERE {search_column} = '{search_value}'
"""
df_search_result = session.sql(query).to_pandas()
if not df_search_result.empty:
st.success("Record found!", icon="✅")
record = df_search_result.iloc[0]
with st.form("update_record"):
mcc_code = st.text_input('MCC Code:', value=record['MCC Code:'])
mcc_title = st.text_input('MCC Title:', value=record['MCC_TITLE'])
ikhokha_defined = st.text_input('iKhokha Defined:', value=record['iKhokha defined (What our customers see)'])
mcc_description = st.text_area('MCC Description:', value=record['MCC_DESCRIPTION'])
included_in_this_mcc = st.text_input('Included in this MCC:', value=record['INCLUDED_IN_THIS_MCC'])
similar_merchants = st.text_input('Similar Merchants:', value=record['SIMILAR_MERCHANTS'])
restricted_cp = st.checkbox('Restricted CP', value=record['RESTRICTED_CP'] == 1)
restricted_cnp = st.checkbox('Restricted CNP', value=record['RESTRICTED_CNP'] == 1)
update_button = st.form_submit_button('Update')
if update_button:
try:
restricted_cp_int = 1 if restricted_cp else 0
restricted_cnp_int = 1 if restricted_cnp else 0
update_query = f"""
UPDATE IK_DEV.POWER_WRITE_BACK.MERCHANT_CATEGORY_CODE_LIST
SET "MCC Code:" = '{mcc_code}', "MCC_TITLE" = '{mcc_title}', "iKhokha defined (What our customers see)" = '{ikhokha_defined}',
"MCC_DESCRIPTION" = '{mcc_description}', "INCLUDED_IN_THIS_MCC" = '{included_in_this_mcc}', "SIMILAR_MERCHANTS" = '{similar_merchants}',
"RESTRICTED_CP" = {restricted_cp_int}, "RESTRICTED_CNP" = {restricted_cnp_int}
WHERE {search_column} = '{search_value}'
"""
session.sql(update_query).collect()
st.success('Record successfully updated!', icon="✅")
except Exception as e:
st.error(f"Error updating record: {e}")
else:
st.error("No record found!", icon="❌")
except Exception as e:
st.error(f"Error searching for record: {e}")
I tried the above but when I check the table no update of the existing record has taken place