bbdd-utils 0.1__py3-none-any.whl → 0.2.0__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 +68 -0
- {bbdd_utils-0.1.dist-info → bbdd_utils-0.2.0.dist-info}/METADATA +1 -1
- bbdd_utils-0.2.0.dist-info/RECORD +7 -0
- bbdd_utils-0.1.dist-info/RECORD +0 -7
- {bbdd_utils-0.1.dist-info → bbdd_utils-0.2.0.dist-info}/WHEEL +0 -0
- {bbdd_utils-0.1.dist-info → bbdd_utils-0.2.0.dist-info}/licenses/LICENSE +0 -0
- {bbdd_utils-0.1.dist-info → bbdd_utils-0.2.0.dist-info}/top_level.txt +0 -0
bbdd_utils/utils.py
CHANGED
@@ -1,2 +1,70 @@
|
|
1
1
|
def saludar(nombre):
|
2
2
|
return f"Hola, {nombre}! Esta es tu librería personalizada."
|
3
|
+
|
4
|
+
|
5
|
+
|
6
|
+
def Conector_bbdd(tipo='sqlite', nombre='mi_base_de_datos', host='localhost', puerto=None, usuario=None, contraseña=None):
|
7
|
+
"""
|
8
|
+
Conecta a una base de datos SQL o NoSQL según el tipo especificado.
|
9
|
+
|
10
|
+
Parámetros:
|
11
|
+
- tipo: 'sqlite', 'mysql' o 'mongodb'
|
12
|
+
- nombre: nombre de la base de datos
|
13
|
+
- host, puerto, usuario, contraseña: solo para MySQL y MongoDB
|
14
|
+
|
15
|
+
Retorna:
|
16
|
+
- Objeto de conexión (SQLite/MySQL) o cliente (MongoDB)
|
17
|
+
"""
|
18
|
+
if tipo == 'sqlite':
|
19
|
+
import sqlite3
|
20
|
+
import os
|
21
|
+
nombre_db = f"{nombre}.db"
|
22
|
+
|
23
|
+
crear_tabla = not os.path.exists(nombre_db)
|
24
|
+
conn = sqlite3.connect(nombre_db)
|
25
|
+
|
26
|
+
if crear_tabla:
|
27
|
+
cursor = conn.cursor()
|
28
|
+
cursor.execute('''
|
29
|
+
CREATE TABLE IF NOT EXISTS usuarios (
|
30
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
31
|
+
nombre TEXT NOT NULL,
|
32
|
+
edad INTEGER NOT NULL
|
33
|
+
)
|
34
|
+
''')
|
35
|
+
conn.commit()
|
36
|
+
|
37
|
+
return conn
|
38
|
+
|
39
|
+
elif tipo == 'mysql':
|
40
|
+
import mysql.connector
|
41
|
+
conn = mysql.connector.connect(
|
42
|
+
host=host,
|
43
|
+
user=usuario,
|
44
|
+
password=contraseña,
|
45
|
+
database=nombre
|
46
|
+
)
|
47
|
+
return conn
|
48
|
+
|
49
|
+
elif tipo == 'mongodb':
|
50
|
+
from pymongo import MongoClient
|
51
|
+
if puerto is None:
|
52
|
+
puerto = 27017
|
53
|
+
cliente = MongoClient(host=host, port=puerto, username=usuario, password=contraseña)
|
54
|
+
return cliente[nombre]
|
55
|
+
|
56
|
+
else:
|
57
|
+
raise ValueError("Tipo de base de datos no soportado. Usa 'sqlite', 'mysql' o 'mongodb'.")
|
58
|
+
|
59
|
+
def cerrar_conexion(conn):
|
60
|
+
"""
|
61
|
+
Cierra la conexión a la base de datos.
|
62
|
+
|
63
|
+
Parámetros:
|
64
|
+
- conn: objeto de conexión
|
65
|
+
|
66
|
+
Retorna:
|
67
|
+
- None
|
68
|
+
"""
|
69
|
+
if conn:
|
70
|
+
conn.close()
|
@@ -0,0 +1,7 @@
|
|
1
|
+
bbdd_utils/__init__.py,sha256=mfT9j4sXdmNbWKXnuAaL1YNWtqmDiUp_C3SepMIqf-4,90
|
2
|
+
bbdd_utils/utils.py,sha256=ry_puN5vEjE6zpMvTjxF7DOpVJu5lbBkydHQwnSgcwk,1978
|
3
|
+
bbdd_utils-0.2.0.dist-info/licenses/LICENSE,sha256=D4uqg6QX0fapLNcdUznOuGaRIUPnypOdeE2zIyYq7xY,217
|
4
|
+
bbdd_utils-0.2.0.dist-info/METADATA,sha256=OA5mDudArgvO0JAScm1sA1FjOwYqpJQm2w8-OeZJLUg,291
|
5
|
+
bbdd_utils-0.2.0.dist-info/WHEEL,sha256=DnLRTWE75wApRYVsjgc6wsVswC54sMSJhAEd4xhDpBk,91
|
6
|
+
bbdd_utils-0.2.0.dist-info/top_level.txt,sha256=bBGkVkxnOw7tZ2zRcyCM3lgVgYo1hFbcmwkGdKAj9Gk,11
|
7
|
+
bbdd_utils-0.2.0.dist-info/RECORD,,
|
bbdd_utils-0.1.dist-info/RECORD
DELETED
@@ -1,7 +0,0 @@
|
|
1
|
-
bbdd_utils/__init__.py,sha256=mfT9j4sXdmNbWKXnuAaL1YNWtqmDiUp_C3SepMIqf-4,90
|
2
|
-
bbdd_utils/utils.py,sha256=pwLnNf5VklEkkgh5bIfRy60lqwdjb1uPVNOK5yIndPo,89
|
3
|
-
bbdd_utils-0.1.dist-info/licenses/LICENSE,sha256=D4uqg6QX0fapLNcdUznOuGaRIUPnypOdeE2zIyYq7xY,217
|
4
|
-
bbdd_utils-0.1.dist-info/METADATA,sha256=8t0SNyP8ZYrhk4CRJX1a2aFEWDBGjy9LRdTZEAyryxM,289
|
5
|
-
bbdd_utils-0.1.dist-info/WHEEL,sha256=DnLRTWE75wApRYVsjgc6wsVswC54sMSJhAEd4xhDpBk,91
|
6
|
-
bbdd_utils-0.1.dist-info/top_level.txt,sha256=bBGkVkxnOw7tZ2zRcyCM3lgVgYo1hFbcmwkGdKAj9Gk,11
|
7
|
-
bbdd_utils-0.1.dist-info/RECORD,,
|
File without changes
|
File without changes
|
File without changes
|