-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate.py
More file actions
29 lines (27 loc) · 1.14 KB
/
Copy pathupdate.py
File metadata and controls
29 lines (27 loc) · 1.14 KB
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
import mysql.connector
from mysql.connector import Error
conexion=None
try:
# nos conectamos con la BD
conexion=mysql.connector.connect(
host='localhost', # hosting
port=3306, # puerto mysql
user='root', # nombre del usuario
password='', # contraseña del usuario
db='pooc2iei' # nombre de la base de datos
)
if conexion.is_connected():
cursor=conexion.cursor() # creando el cursor
cursor.execute("update persona set edad = 20 where id=2")
# cursor.execute("update persona set edad = 19,nombres='Jorge' where id=1")
# si fuese mas de un registro, seria set campo1 = dato1,campo2 = dato2 ....where condicion
# si la linea de codigo es muy extensa, puedes escribirlas en dos o más lineas hacia abajo
# poniendo """ (3 comillas al inicio) y """ (3 comillas al final)
conexion.commit() # confirma la query realizada
print("Registro actualizado exitosamente !!!")
except Error as ex:
print("Ha ocurrido un error:",ex)
finally:
if conexion.is_connected():
conexion.close()
print("Conexión cerrada")