-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimpleSQL.py
More file actions
104 lines (78 loc) · 3.11 KB
/
Copy pathsimpleSQL.py
File metadata and controls
104 lines (78 loc) · 3.11 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
##################################
""" SimpleSQL """
##################################
from sanitizar import sanitizar
class IniciarBD:
def __init__(self,name):
try:
import sqlite3
self.name=sanitizar(name)
self.conexion = sqlite3.connect(self.name)
self.cursor = self.conexion.cursor()
with open("BD_Rejistro.txt","w") as f:
f.write(self.name)
except Exception as e:
print(f"Error: {e}")
print(f"Exito al Conectar con {name}")
def CrearTabla(self, name, *campos):
try:
name=sanitizar(name)
campos=sanitizar(campos)
campos=",".join(campos)
self.cursor.execute(f"CREATE TABLE IF NOT EXISTS {name} (id INTEGER PRIMARY KEY AUTOINCREMENT,{campos})")
print_campos=" ".join(campos.split())
print(print_campos)
except Exception as e:
print(f"Error: {e}")
print(f"Exito al crear la tabla {name}")
def InsertarDatos(self, name, campos, *valores):
try:
name=sanitizar(name)
campos=sanitizar(campos)
valores=sanitizar(valores)
placeholders = ', '.join(['?'] * len(valores))
self.cursor.execute(f"INSERT INTO {name} ({campos}) VALUES ({placeholders})", valores)
self.conexion.commit()
except Exception as e:
print(f"Error: {e}")
print(f"Exito al insertar los datos")
def EliminarTabla(self, name):
try:
name=sanitizar(name)
self.cursor.execute(f"DROP TABLE IF EXISTS {name}")
except Exception as e:
print(f"Error: {e}")
print(f"Exito al eliminar la tabla {name}")
def ConsultarDatos(self,name):
try:
name=sanitizar(name)
self.cursor.execute(f"SELECT * FROM {name}")
filas=[]
resultados = self.cursor.fetchall()
for fila in resultados:
filas.append(fila)
print(fila)
except Exception as e:
print(f"Error: {e}")
return filas
def Actualizar(self, name,dato,condicion):
name=sanitizar(name)
dato=sanitizar(dato)
condicion=sanitizar(condicion)
try:
self.cursor.execute(f"UPDATE {name} SET {dato} WHERE {condicion} ")
self.cursor.commit()
except Exception as e:
print(f"Error: {e}")
print("Exito al actualizar")
def Cerrar(self):
try:
self.conexion.close()
except Exception as e:
print(f"Error: {e}")
print("Conexion cerrada con exito")
db=IniciarBD("user")
db.CrearTabla("users","user TEXT NOT NULL")
db.InsertarDatos("users","user","Elvin@hotmail.com")
db.ConsultarDatos("users")
db.Cerrar()