-1

I'm working with streamlit, pandas, Mysql and AG-grid I've got some help here before which I'm very grateful but I encountered another issue when I moved from SQLite to MySQL

my goal is to create a data frame in my website and to update the table with ag-grid straight into the DB (MySQL DB) i don't want the user to edit all the parameters all the time, sometimes the user wants to edit 1 parameter or 3 parameters but when i try to edit 1 parameter i get this error: mysql.connector.errors.ProgrammingError: Not all parameters were used in the SQL statement actually, even when i try to edit all the parameters i still get the same error this is the table:

This is the table

this is the part of the code:

       crud(mydb, mycursor)


    def update(ID, Results, Run_By, FW_Mode, NICS, SXL_Mode, SW_Blades, Machine, Test, Descrip):
        mycursor.execute('UPDATE LightSpeed SET Results=?, Run_By=?,FW_Mode=?,NICS=?,SXL_Mode=?,SW_Blades=?,Machine=?,Test=?,Descrip=? WHERE ID=?  ', (Results, Run_By, FW_Mode, NICS, SXL_Mode, SW_Blades, Machine, Test, Descrip, ID))
        mydb.commit()




    st.write('##### Intital contents of db')
    st.write(
        'To edit please mark single or multiple, after that mark the id of the chosen row and edit, confrim by pressing Update db')
    df = pd.read_sql('SELECT * FROM LightSpeed', con=mydb)

    gd = GridOptionsBuilder.from_dataframe(df)
    gd.configure_pagination(enabled=True)
    gd.configure_default_column(editable=True, groupable=True)
    sel_mode = st.radio('Selection Type', options=['single', 'multiple'])
    gd.configure_selection(selection_mode=sel_mode, use_checkbox=True)
    gridoptions = gd.build()
    grid_table = AgGrid(df, gridOptions=gridoptions,
                        update_mode=GridUpdateMode.SELECTION_CHANGED | GridUpdateMode.VALUE_CHANGED,
                        height=500,
                        allow_unsafe_jscode=True,
                        # enable_enterprise_modules = True,
                        theme='fresh')

    sel_row = grid_table["selected_rows"]

    df_selected = pd.DataFrame(sel_row)

    if st.button('Update db', key=1):
        for i, r in df_selected.iterrows():
            ID = r['ID']
            Results = r['Results']
            Run_By = r['Run_By']
            FW_Mode = r['FW_Mode']
            NICS = r['NICS']
            SXL_Mode = r['SXL_Mode']
            SW_Blades = r['SW_Blades']
            Machine = r['Machine']
            Test = r['Test']
            Descrip = r['Descrip']
            update(ID, Results, Run_By, FW_Mode, NICS, SXL_Mode, SW_Blades, Machine, Test, Descrip)

        st.write('##### Updated db')
        df_update = pd.read_sql('SELECT * FROM LightSpeed', con=mydb)
        st.write(df_update)

    mycursor.close()
    mydb.close()

Thank you!!

EDIT: with the help of @Luuk, I changed the code but it still doesn't update the DB

    ID = "%s"
    Results = "%s"
    Run_By = "%s"
    FW_Mode = "%s"
    NICS= "%s"
    SXL_Mode= "%s"
    SW_Blades= "%s"
    Machine= "%s"
    Test= "%s"
    Descrip= "%s"

    data_update = (ID, Results, Run_By, FW_Mode, NICS, SXL_Mode, SW_Blades, Machine, Test, Descrip)

    update_data = """(UPDATE LightSpeed SET Results, Run_By,FW_Mode,NICS,SXL_Mode,SW_Blades,Machine,Test,Descrip WHERE ID)  VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s))""" % data_update
Dor Elia
  • 1
  • 5
  • "This is the table" ==> NO, it's a simple image, definitely not a table... – Luuk May 22 '22 at 08:10
  • my bad about the image still cant figure out what to do.. – Dor Elia May 22 '22 at 08:24
  • The default paramstyle of MySQL seems to be [pyformat](https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysql-connector-paramstyle.html). (see: https://peps.python.org/pep-0249/#paramstyle). You might need to change the `?` in your prepared statement, or change the paramstyle. – Luuk May 22 '22 at 08:45

0 Answers0