-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwrite_data_mysql.py
More file actions
43 lines (40 loc) · 1.42 KB
/
write_data_mysql.py
File metadata and controls
43 lines (40 loc) · 1.42 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import mysql.connector
import random
from mysql.connector import Error
def conectar():
"""Conecta al servidor MySQL."""
try:
conexion = mysql.connector.connect(
host='127.0.0.1',
user='admin',
password='1234',
database='bd_almacen'
)
if conexion.is_connected():
print('Conexión exitosa.')
return conexion
except Error as e:
print(f"Error al conectar a MySQL: {e}")
def insertar_datos(conexion):
try:
cursor = conexion.cursor()
for i in range(50):
#consulta_sql = "INSERT INTO almacen(id_producto, producto,precio) VALUES (%s,%s,%s )"
consulta_sql = "INSERT INTO almacen(id_producto,producto,precio) VALUES (%s,%s,%s )"
#consulta_sql = ("UPDATE bd_almacen.almacen SET producto = (%s) WHERE (id_producto = (%s))")
#datos = (str(''.join(f"dataasd {i}")), i+8)
datos = (i,str(''.join(f"data {i}")), random.randint(0, 200))
cursor.execute(consulta_sql, datos)
conexion.commit()
print(f"Dato {i} actualizado correctamente.")
except Error as e:
print(f"Error al insertar datos: {e}")
finally:
if conexion.is_connected():
cursor.close()
conexion.close()
print('Conexión cerrada.')
# Uso de las funciones
conexion = conectar()
if conexion:
insertar_datos(conexion)