Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

python mysql.connector, not running sql code

The code is calling a database and producing a table, this function is calling what is selected and running a sql query to swap the exceptions int and move it around in the tables, this works when hard coded into it

def selectedRow():
    # Get the currently active tab
    active_tab = tab_control.tab(tab_control.select(), "text")
     
    if active_tab == "Table 1":
        tree = tab1.winfo_children()[0]  # The first child of tab1 should be the Treeview
    elif active_tab == "Table 2":
        tree = tab2.winfo_children()[0]  # The first child of tab2 should be the Treeview
    elif active_tab == "Table 3":
        tree = tab3.winfo_children()[0]  # The first child of tab3 should be the Treeview
    else:
        print("No valid tab selected.")
        return
    # Get the currently selected item in the Treeview
    curItem = tree.focus()
    if not curItem:
        print("No row selected.")
        return

    # Get the data of the selected row and print them to the console
    contents = tree.item(curItem)
    print(contents['values'])
    conn, cursor = connect_to_database()
    exceptions = contents['values'][4]
    filepath = (contents['values'][1],)
    Ex_to_Z = "update tableoffiles set Exceptions = 0 where DataLocation = %s"
    drop_f_ex = "Delete From tableofexceptions where DataLocation = %s"
    Ex_to_O = "update tableoffiles set Exceptions = 1 where DataLocation = %s"
    add_f_ex = "Insert into tableofexceptions select * from tableoffiles where DataLocation = %s"
    drop_f_sum = "Delete From tableofsum where DataLocation = %s"
    add_to_sum = "Insert into tableofsum select * from tableoffiles where DataLocation = %s"
    if exceptions == 1:
        print(filepath)
        cursor.execute(Ex_to_Z, filepath)
        print("setting exept to 0")
        cursor.execute(drop_f_ex, filepath)
        print("add to except")
        cursor.execute(add_to_sum, filepath)
        print("adding to sum")
        refresh_tables()
        print("refreshing tables")

    elif exceptions == 0:
        print(filepath)
        cursor.execute(Ex_to_O, filepath)
        print("setting exept to 1")
        cursor.execute(add_f_ex, filepath)
        print("add to except")
        cursor.execute(drop_f_sum, filepath)
        print("removing from sum")
        refresh_tables()
        print("refreshing tables")

    else:
        print("invalid exception")
        return


# Exception Button: Update the command to call selectedRow()
Exception_button = ttk.Button(window, text="exeptions swap", command=selectedRow)
Exception_button.pack()

— output (exept = 1) —

[‘HART0001’, ‘C:/Users/train/7.xls\n’, 0, 0, 1] , (‘C:/Users/train/7.xls\n’,)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

setting exept to 0,
add to except,
adding to sum,
refreshing tables

— output (exept = 0) —

[‘HART0002’, ‘C:/Users/Michael/CODE/2.xls’, 0, 1, 0] ,
(‘C:/Users/Michael/CODE/2.xls’,) ,
setting exept to 1 ,
add to except ,
removing from sum ,
refreshing tables

At the moment its not changing the database and i am not sure why, any help would be appreciate

>Solution :

for insert, update and delete queries you need to execute conn.commit() to commit the change to the database

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading