Python - Problema con UPDATE en tkinter

 
Vista:
Imágen de perfil de Tomas

Problema con UPDATE en tkinter

Publicado por Tomas (7 intervenciones) el 26/12/2021 10:20:31
Tengo un problema al hacer una actualización de datos en la base de datos externa,

Tengo este código que si lo ejecuto en consola funciona perfectamente.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import pymysql
 
con = pymysql.connect(host='uuu',
                             user='uuu',
                             password='uuu',
                             database='uuu',
                             charset='utf8mb4',
                             cursorclass=pymysql.cursors.DictCursor)
 
city = (14, 'rivera', 2007654)
city1 = (21, 'Alcala2', 32276004)
nombre = "tary"
correo = "prueba@prueba.com"
 
def crearCliente():
    try:
        cur = con.cursor()
        crearCliente = "UPDATE test1 SET name='"+nombre+"', email='"+correo+"' WHERE id = 19"
        cur.execute(crearCliente)
        con.commit()
 
        #con.close()
 
        print("Se guardo correctamente")
    except:
        print ("Error")
 
    finally:
        try:
            con.close()
        except:
            None
 
 
def mostrarCliente():
    try:
        con1 = pymysql.connect(host='uuu',
                             user='uuu',
                             password='uuu',
                             database='uuu',
                             charset='utf8mb4',
                             cursorclass=pymysql.cursors.DictCursor)
 
 
        cur1 = con1.cursor()
        mostrarCliente = "SELECT * FROM test1 WHERE id = 19"
        cur1.execute(mostrarCliente)
        rows = cur1.fetchall()
 
        #con.close()
 
        print("El cliente es:", rows)
    except:
        print("Error2")
 
    finally:
        try:
            con1.close()
        except:
            None
 
 
 
 
crearCliente()
mostrarCliente()
print ("Se ha guardado")

Pero al hacer el código en tkinter, se ejecuta el aplauso perfecto, y hago clic en el botón y sale que se ha actualizado, pero miro la base de datos y no se ha cambiado nada.

Sin embargo, si ejecuto el código en la consola si funciona y se realiza el cambio, pero quiero hacerlo en una aplicación gráfica.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
from tkinter import *
from tkinter import ttk
import pymysql
 
####### FUNCIONES #######
 
city = (9, 'Kiev', 2887000)
 
nombre = "TARY"
correo = "tary@tary.com"
 
def actualizarCliente():
    try:
        con2 = pymysql.connect(host='uuu',
                             user='uuu',
                             password='uuu',
                             database='uuu',
                             charset='utf8mb4',
                             cursorclass=pymysql.cursors.DictCursor)
 
 
        cur2 = con2.cursor()
        actualizarCliente = "UPDATE test1 SET name='tomas' WHERE id = 19"
        cur2.execute(actualizarCliente)
 
        #con.close()
 
        ttk.Label(mainframe, text="Se actualizo el cliente").grid(column=40, row=150, sticky=W)
    except:
        print("Error2")
 
    finally:
        try:
            con2.close()
        except:
            None
 
def mostrarCliente():
    try:
        con1 = pymysql.connect(host='uuu',
                             user='uuu',
                             password='uuu',
                             database='uuu',
                             charset='utf8mb4',
                             cursorclass=pymysql.cursors.DictCursor)
 
 
        cur1 = con1.cursor()
        mostrarCliente = "SELECT * FROM test1 WHERE id = 19"
        cur1.execute(mostrarCliente)
        rows = cur1.fetchall()
 
        #con.close()
 
        ttk.Label(mainframe, text=rows).grid(column=40, row=150, sticky=W)
    except:
        print("Error2")
 
    finally:
        try:
            con1.close()
        except:
            None
 
 
 
####### VENTANA Tkinter #######
 
root = Tk()
root.title("Mostrar datos")
root.geometry("400x400")
 
mainframe = ttk.Frame(root, padding="3 3 12 12")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)
 
##### Textos y Botones #####
 
ttk.Label(mainframe, text="nada").grid(column=3, row=2, sticky=W)
 
ttk.Button(mainframe, text="MOSTRAR CLIENTE", command=mostrarCliente).grid(column=40, row=40, sticky=W)
ttk.Button(mainframe, text="ACTUALIZAR CLIENTE", command=actualizarCliente).grid(column=40, row=60, sticky=W)
 
 
root.mainloop()

¿Qué esta mal para que no se haga efectivo el cambio?
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder