Mostrando entradas con la etiqueta inserta. Mostrar todas las entradas
Mostrando entradas con la etiqueta inserta. Mostrar todas las entradas

jueves, 22 de febrero de 2018

31 Python. Agenda de contactos parte 2

Como veras, en esta entrada, creamos el modulo que ejecutara las funciones necesarias para el manejo de la base de datos Sqlite (creacion, insercion, modificacion, borrado y busqueda).
Para ello creamos un nuevo archivo (BDatos).



El codigo resultante de los 2 archivos es el siguiente:

BDatos
#SQLITE
import sqlite3
#CREA TABLA
def creaTabla():
    conexion=sqlite3.connect("agenda.db")
    consulta=conexion.cursor()
    sql="""CREATE TABLE IF NOT EXISTS agenda(id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
 nombre VARCHAR(20) NOT NULL, apellidos VARCHAR(20) NOT NULL, telefono VARCHAR(14) NOT NULL,
 email VARCHAR(20) NOT NULL)"""
    if(consulta.execute(sql)):
        print("Tabla creada")
    else:
        print("No se pudo crear la tabla")
    conexion.close()
#INSERTAR DATOS
def inserta(nombre,apellidos,telefono,email):
    conexion=sqlite3.connect("agenda.db")
    consulta=conexion.cursor()
    datos=(nombre,apellidos,telefono,email)
    sql="""INSERT INTO agenda(nombre,apellidos,telefono,email) VALUES (?,?,?,?)"""
    if(consulta.execute(sql,datos)):
         print("Datos guardados")
    else:
        print("No se pudo guardar el dato")
    conexion.commit()
    conexion.close()

Agenda

 #AGENDA PYTHON
#CON SQLITE
#IMPORTA MODULO
from tkinter import *
from BDatos import *
from tkinter import messagebox
#VARIABLES
ANCHO=560
ALTO=540
POSX=400
POSY=400
anchoAlto=(str(ANCHO)+"x"+str(ALTO))
posicionX="+"+str(POSX)
posicionY="+"+str(POSY)
colorVentana="blue"
colorFondo="blue"
colorLetra="white"
#funcion pruebas
def mostrarMensaje():
    print("Pruebas")
#FUNCIONES
def limpiar():
    ID.set("")
    nombre.set("")
    apellidos.set("")
    telefono.set("")
    email.set("")
def guardar():
    no=nombre.get()
    ap=apellidos.get()
    tf=telefono.get()
    em=email.get()
    if((no=="")or(ap=="")):
        print("Faltan datos")
    else:
        limpiar()
        creaTabla()
        inserta(no,ap,tf,em)
        print("Datos guardados")
#VENTANA
ventana=Tk()
ventana.config(bg=colorVentana)
ventana.geometry(anchoAlto+posicionX+posicionY)
ventana.title("Agenda")
#Variables cajas
ID=IntVar()
nombre=StringVar()
apellidos=StringVar()
telefono=StringVar()
email=StringVar()
#Widgets
etiquetaID=Label(ventana,text="ID:").place(x=50,y=50)
cajaID=Entry(ventana,textvariable=ID).place(x=130,y=50)
etiquetaNombre=Label(ventana,text="Nombre:").place(x=50,y=90)
cajaNombre=Entry(ventana,textvariable=nombre).place(x=130,y=90)
etiquetaApellidos=Label(ventana,text="Apellidos:").place(x=50,y=130)
cajaApellidos=Entry(ventana,textvariable=apellidos).place(x=130,y=130)
etiquetaTelefono=Label(ventana,text="Telefono:").place(x=50,y=170)
cajaTelefono=Entry(ventana,textvariable=telefono).place(x=130,y=170)
etiquetaEmail=Label(ventana,text="Email:").place(x=50,y=210)
cajaEmail=Entry(ventana,textvariable=email).place(x=130,y=210)
textLista=Text(ventana)
textLista.place(x=50,y=240,width=400,height=200)
#Botones
botonAñadir=Button(ventana,text="Añadir",command=guardar).place(x=150,y=500)
botonBorrar=Button(ventana,text="Borrar",command=mostrarMensaje).place(x=200,y=500)
botonBuscar=Button(ventana,text="Buscar",command=mostrarMensaje).place(x=250,y=500)
botonModificar=Button(ventana,text="Modificar",command=mostrarMensaje).place(x=300,y=500)

ventana.mainloop()