bbdd-utils 0.3.9__tar.gz → 0.3.10__tar.gz
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-0.3.9 → bbdd_utils-0.3.10}/PKG-INFO +1 -1
- {bbdd_utils-0.3.9 → bbdd_utils-0.3.10}/bbdd_utils/utils.py +32 -22
- {bbdd_utils-0.3.9 → bbdd_utils-0.3.10}/bbdd_utils.egg-info/PKG-INFO +1 -1
- {bbdd_utils-0.3.9 → bbdd_utils-0.3.10}/setup.py +1 -1
- {bbdd_utils-0.3.9 → bbdd_utils-0.3.10}/LICENSE +0 -0
- {bbdd_utils-0.3.9 → bbdd_utils-0.3.10}/README.md +0 -0
- {bbdd_utils-0.3.9 → bbdd_utils-0.3.10}/bbdd_utils/__init__.py +0 -0
- {bbdd_utils-0.3.9 → bbdd_utils-0.3.10}/bbdd_utils.egg-info/SOURCES.txt +0 -0
- {bbdd_utils-0.3.9 → bbdd_utils-0.3.10}/bbdd_utils.egg-info/dependency_links.txt +0 -0
- {bbdd_utils-0.3.9 → bbdd_utils-0.3.10}/bbdd_utils.egg-info/top_level.txt +0 -0
- {bbdd_utils-0.3.9 → bbdd_utils-0.3.10}/pyproject.toml +0 -0
- {bbdd_utils-0.3.9 → bbdd_utils-0.3.10}/setup.cfg +0 -0
@@ -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,38 @@ 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
|
+
# Crear la tabla dinámicamente si no existe
|
78
|
+
columnas_definicion = ", ".join([f"{col} TEXT" for col in columnas])
|
79
|
+
cursor.execute(f"""
|
80
|
+
CREATE TABLE IF NOT EXISTS {tabla} (
|
81
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
82
|
+
{columnas_definicion}
|
83
|
+
)
|
84
|
+
""")
|
85
|
+
print(f"Tabla '{tabla}' verificada o creada exitosamente.")
|
86
|
+
|
87
|
+
# Inserta datos en la tabla
|
88
|
+
placeholders = ", ".join(["?" for _ in columnas])
|
89
|
+
cursor.execute(f"INSERT INTO {tabla} ({', '.join(columnas)}) VALUES ({placeholders})", valores)
|
89
90
|
conn.commit()
|
90
|
-
print("Datos insertados correctamente en la base de datos SQL.")
|
91
|
+
print(f"Datos insertados correctamente en la tabla '{tabla}' de la base de datos SQL.")
|
91
92
|
|
92
93
|
# Consulta los datos insertados
|
93
|
-
cursor.execute("SELECT * FROM
|
94
|
+
cursor.execute("SELECT * FROM {tabla}")
|
94
95
|
resultados = cursor.fetchall()
|
95
96
|
for fila in resultados:
|
96
97
|
print(f"ID: {fila[0]}, Nombre: {fila[1]}, Edad: {fila[2]}")
|
@@ -100,8 +101,17 @@ def Insert(conn, data):
|
|
100
101
|
print("Conexión exitosa a la base de datos MySQL.")
|
101
102
|
# Si estamos usando MySQL, podemos trabajar con la conexión de esta manera
|
102
103
|
cursor = conn.cursor()
|
104
|
+
# Verificar si la tabla 'usuarios' existe y crearla si no
|
105
|
+
cursor.execute("""
|
106
|
+
CREATE TABLE IF NOT EXISTS {tabla} (
|
107
|
+
id INT AUTO_INCREMENT PRIMARY KEY,
|
108
|
+
nombre VARCHAR(255) NOT NULL,
|
109
|
+
edad INT NOT NULL
|
110
|
+
)
|
111
|
+
""")
|
112
|
+
print("Tabla 'usuarios' verificada o creada exitosamente.")
|
103
113
|
# Inserta datos en la tabla 'usuarios'
|
104
|
-
cursor.execute("INSERT INTO
|
114
|
+
cursor.execute("INSERT INTO {tabla} (nombre, edad) VALUES (%s, %s)", data)
|
105
115
|
conn.commit()
|
106
116
|
print("Datos insertados correctamente en la base de datos MySQL.")
|
107
117
|
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|