Created
September 28, 2017 05:09
-
-
Save parzibyte/bd9479c08236bfe8c659c1fba475ff9f to your computer and use it in GitHub Desktop.
SQLite3 interactivo con Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Abre una base de datos de sqlite3 e interactúa con ella. | |
Útil para hacer pruebas | |
@author parzibyte | |
""" | |
import sqlite3 | |
while True: | |
try: | |
""" | |
Tratar de abrir la base de datos e imprimir | |
cualquier error que pase al hacerlo | |
Comprobar también si el usuario quiere salir completamente | |
del programa | |
""" | |
nombre_base_de_datos = input("Escribe el nombre de la base de datos: [-1 para salir]\t") | |
if nombre_base_de_datos == "-1": | |
break | |
conexion = sqlite3.connect(nombre_base_de_datos) | |
cursor = conexion.cursor() | |
print("Base de datos abierta!") | |
while True: | |
""" | |
Leer la entrada del usuario y salir si así lo desea. | |
Si no, ejecutar la consulta, mostrar los resultados | |
y si hay algún error imprimirlo | |
""" | |
try: | |
sentencia = input("Escribe la consulta que deseas ejecutar: [-1 para cambiar la base de datos]\n\t") | |
if sentencia == "-1": | |
break | |
resultado = cursor.execute(sentencia) | |
datos = cursor.fetchall() | |
conexion.commit() | |
print("Los datos que trajo la consulta son: ") | |
for dato in datos: | |
print(dato) | |
except sqlite3.OperationalError as error: | |
print("Error al ejecutar la consulta: ", error) | |
except sqlite3.OperationalError as error: | |
print("Error al abrir la base de datos: ", error) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment