bbdd-utils 0.3.9__py3-none-any.whl → 0.3.11__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- bbdd_utils/utils.py +40 -23
- {bbdd_utils-0.3.9.dist-info → bbdd_utils-0.3.11.dist-info}/METADATA +1 -1
- bbdd_utils-0.3.11.dist-info/RECORD +7 -0
- bbdd_utils-0.3.9.dist-info/RECORD +0 -7
- {bbdd_utils-0.3.9.dist-info → bbdd_utils-0.3.11.dist-info}/WHEEL +0 -0
- {bbdd_utils-0.3.9.dist-info → bbdd_utils-0.3.11.dist-info}/licenses/LICENSE +0 -0
- {bbdd_utils-0.3.9.dist-info → bbdd_utils-0.3.11.dist-info}/top_level.txt +0 -0
bbdd_utils/utils.py
CHANGED
@@ -16,22 +16,8 @@ def Conector_bbdd(tipo='sqlite', nombre='mi_base_de_datos', host='localhost', pu
|
|
16
16
|
if tipo == 'sqlite':
|
17
17
|
import sqlite3
|
18
18
|
import os
|
19
|
-
nombre_db = f"{nombre}.db"
|
20
|
-
|
21
|
-
crear_tabla = not os.path.exists(nombre_db)
|
19
|
+
nombre_db = f"{nombre}.db"
|
22
20
|
conn = sqlite3.connect(nombre_db)
|
23
|
-
|
24
|
-
if crear_tabla:
|
25
|
-
cursor = conn.cursor()
|
26
|
-
cursor.execute('''
|
27
|
-
CREATE TABLE IF NOT EXISTS usuarios (
|
28
|
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
29
|
-
nombre TEXT NOT NULL,
|
30
|
-
edad INTEGER NOT NULL
|
31
|
-
)
|
32
|
-
''')
|
33
|
-
conn.commit()
|
34
|
-
|
35
21
|
return conn
|
36
22
|
|
37
23
|
elif tipo == 'mysql':
|
@@ -74,23 +60,45 @@ def Cerrar_conexion(conn):
|
|
74
60
|
if conn:
|
75
61
|
conn.close()
|
76
62
|
|
77
|
-
def Insert(conn, data):
|
63
|
+
def Insert(conn, tabla, data):
|
78
64
|
# Verifica el tipo de base de datos con la que estamos trabajando
|
79
65
|
import sqlite3
|
80
66
|
import mysql.connector
|
81
67
|
from pymongo.database import Database
|
68
|
+
|
69
|
+
# Extraer las claves y valores del diccionario
|
70
|
+
columnas = data.keys()
|
71
|
+
valores = tuple(data.values())
|
72
|
+
|
82
73
|
############# SQLite ################
|
83
74
|
if isinstance(conn, sqlite3.Connection):
|
84
75
|
print("Conexión exitosa a la base de datos SQL.")
|
85
|
-
# Si estamos usando SQLite, podemos trabajar con la conexión de esta manera
|
86
76
|
cursor = conn.cursor()
|
87
|
-
|
88
|
-
|
77
|
+
|
78
|
+
# Crear la tabla dinámicamente si no existe
|
79
|
+
columnas_definicion = ", ".join([f"{col} TEXT" for col in columnas])
|
80
|
+
cursor.execute(f"""
|
81
|
+
CREATE TABLE IF NOT EXISTS {tabla} (
|
82
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
83
|
+
{columnas_definicion}
|
84
|
+
)
|
85
|
+
""")
|
86
|
+
print(f"Tabla '{tabla}' verificada o creada exitosamente.")
|
87
|
+
|
88
|
+
# Si `data` es un solo diccionario, conviértelo en una lista
|
89
|
+
if isinstance(data, dict):
|
90
|
+
data = [data]
|
91
|
+
# Inserta cada elemento de la lista en la tabla
|
92
|
+
for registro in data:
|
93
|
+
columnas = ", ".join(registro.keys())
|
94
|
+
valores = ", ".join(["?"] * len(registro))
|
95
|
+
query = f"INSERT INTO {tabla} ({columnas}) VALUES ({valores})"
|
96
|
+
cursor.execute(query, tuple(registro.values()))
|
89
97
|
conn.commit()
|
90
|
-
print("Datos insertados correctamente en
|
91
|
-
|
98
|
+
print("Datos insertados correctamente en SQLite.")
|
99
|
+
|
92
100
|
# Consulta los datos insertados
|
93
|
-
cursor.execute("SELECT * FROM
|
101
|
+
cursor.execute("SELECT * FROM {tabla}")
|
94
102
|
resultados = cursor.fetchall()
|
95
103
|
for fila in resultados:
|
96
104
|
print(f"ID: {fila[0]}, Nombre: {fila[1]}, Edad: {fila[2]}")
|
@@ -100,8 +108,17 @@ def Insert(conn, data):
|
|
100
108
|
print("Conexión exitosa a la base de datos MySQL.")
|
101
109
|
# Si estamos usando MySQL, podemos trabajar con la conexión de esta manera
|
102
110
|
cursor = conn.cursor()
|
111
|
+
# Verificar si la tabla 'usuarios' existe y crearla si no
|
112
|
+
cursor.execute("""
|
113
|
+
CREATE TABLE IF NOT EXISTS {tabla} (
|
114
|
+
id INT AUTO_INCREMENT PRIMARY KEY,
|
115
|
+
nombre VARCHAR(255) NOT NULL,
|
116
|
+
edad INT NOT NULL
|
117
|
+
)
|
118
|
+
""")
|
119
|
+
print("Tabla 'usuarios' verificada o creada exitosamente.")
|
103
120
|
# Inserta datos en la tabla 'usuarios'
|
104
|
-
cursor.execute("INSERT INTO
|
121
|
+
cursor.execute("INSERT INTO {tabla} (nombre, edad) VALUES (%s, %s)", data)
|
105
122
|
conn.commit()
|
106
123
|
print("Datos insertados correctamente en la base de datos MySQL.")
|
107
124
|
|
@@ -0,0 +1,7 @@
|
|
1
|
+
bbdd_utils/__init__.py,sha256=mjRX7GqWkOJq66KqghZ4MZE0Aai6R89LPeu1mjq4lD4,121
|
2
|
+
bbdd_utils/utils.py,sha256=-kwBRLXIDvdab7WGuQ8utC353K9dd1zv3wl2IgZ10ek,5489
|
3
|
+
bbdd_utils-0.3.11.dist-info/licenses/LICENSE,sha256=D4uqg6QX0fapLNcdUznOuGaRIUPnypOdeE2zIyYq7xY,217
|
4
|
+
bbdd_utils-0.3.11.dist-info/METADATA,sha256=46nYxEfG8DDDuP24kFCaJxjqRfEdeU-i70ugcKzxdq4,694
|
5
|
+
bbdd_utils-0.3.11.dist-info/WHEEL,sha256=DnLRTWE75wApRYVsjgc6wsVswC54sMSJhAEd4xhDpBk,91
|
6
|
+
bbdd_utils-0.3.11.dist-info/top_level.txt,sha256=bBGkVkxnOw7tZ2zRcyCM3lgVgYo1hFbcmwkGdKAj9Gk,11
|
7
|
+
bbdd_utils-0.3.11.dist-info/RECORD,,
|
@@ -1,7 +0,0 @@
|
|
1
|
-
bbdd_utils/__init__.py,sha256=mjRX7GqWkOJq66KqghZ4MZE0Aai6R89LPeu1mjq4lD4,121
|
2
|
-
bbdd_utils/utils.py,sha256=y6MXdvqEqdNVGhhhlKCawnFMC7RREPmbxWIafg1PgpE,4724
|
3
|
-
bbdd_utils-0.3.9.dist-info/licenses/LICENSE,sha256=D4uqg6QX0fapLNcdUznOuGaRIUPnypOdeE2zIyYq7xY,217
|
4
|
-
bbdd_utils-0.3.9.dist-info/METADATA,sha256=3p0CNjKMid_i1-qdW4-MjcJylzXE9Zl289m4BojsCnA,693
|
5
|
-
bbdd_utils-0.3.9.dist-info/WHEEL,sha256=DnLRTWE75wApRYVsjgc6wsVswC54sMSJhAEd4xhDpBk,91
|
6
|
-
bbdd_utils-0.3.9.dist-info/top_level.txt,sha256=bBGkVkxnOw7tZ2zRcyCM3lgVgYo1hFbcmwkGdKAj9Gk,11
|
7
|
-
bbdd_utils-0.3.9.dist-info/RECORD,,
|
File without changes
|
File without changes
|
File without changes
|