Python: Tkinter + SQLITE. Save records to database and clear field

0

I'm studying, on my own, SQlite, I've already bought some courses, but I need some help.

Purpose: I would like to save the user-entered fields in the database and then clear the same fields to allow new data entries.

The program is a window with a field called email and another field called password and a save button.

When you click on the save button, three columns (auto-increment id, email, password) must be saved in the database

The code below shows only the window code, Below is the code for the users module, where I put the SQLite code.

from tkinter import *
import usuarios


janela = Tk()
janela.title("Cadastro de usuário")
janela["bg"] = "lightblue"
janela.geometry('300x200+700+400')

rotulo = Label(janela,
               font="Arial 18 bold",
               text='Cadastro de usuário',
               bg="lightblue")
rotulo.place(x=25, y=10)

rotulo = Label(janela,
               font="Arial 10",
               text='E-mail:',
               bg="lightblue")
rotulo.place(x=25, y=70)

cxtexto1 = Entry(janela,
                 width=20,
                 font="Arial 12 bold")
cxtexto1.place(x=75, y=70)
email = cxtexto1.get()

rotulo = Label(janela,
               font="Arial 10",
               text='Senha:',
               bg="lightblue")
rotulo.place(x=25, y=100)

cxtexto2 = Entry(janela,
                 width=20,
                 font="Arial 12 bold")
cxtexto2.place(x=75, y=100)
senha = cxtexto2.get()

bt2 = Button(janela,
             width=10,
             text="Salvar",
             font="Arial 12 bold",
             command=usuarios.cria_bd)
bt2.place(x=150, y=130)


janela.mainloop()

The code below shows the command to save inside Sqlite

import sqlite3

def cria_bd(email, senha):

    conn = sqlite3.connect('usuarios.db')
    bd = conn.cursor()
    bd.execute("""
    CREATE TABLE IF NOT EXISTS Cadastro (
        id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
        Email TEXT NOT NULL,
        Senha TEXT NOT NULL);""")

    bd.execute("""
    INSERT INTO Cadastro (Email, Senha)
    VALUES (?,?)
    """, (email, senha))

    conn.commit()
    conn.close()
    return
    
asked by anonymous 20.11.2018 / 13:25

1 answer

1

You just need to add a method to perform these operations:

def salvar():
    email = cxtexto1.get()
    senha = cxtexto2.get()
    usuarios.cria_bd(email, senha)
    cxtexto1.delete(0, END)
    cxtexto2.delete(0, END)

Using your example code we have:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
""""""
from tkinter import *
import usuarios


def salvar():
    email = cxtexto1.get()
    senha = cxtexto2.get()
    usuarios.cria_bd(email, senha)
    cxtexto1.delete(0, END)
    cxtexto2.delete(0, END)


janela = Tk()
janela.title("Cadastro de usuário")
janela["bg"] = "lightblue"
janela.geometry('300x200+700+400')

rotulo = Label(janela, font="Arial 18 bold", text='Cadastro de usuário', bg="lightblue")
rotulo.place(x=25, y=10)

rotulo = Label(janela, font="Arial 10", text='E-mail:', bg="lightblue")
rotulo.place(x=25, y=70)

cxtexto1 = Entry(janela, width=20, font="Arial 12 bold")
cxtexto1.place(x=75, y=70)

rotulo = Label(janela, font="Arial 10", text='Senha:', bg="lightblue")
rotulo.place(x=25, y=100)

cxtexto2 = Entry(janela, width=20, font="Arial 12 bold")
cxtexto2.place(x=75, y=100)

bt2 = Button(janela, width=10, text="Salvar", font="Arial 12 bold", command=salvar)
bt2.place(x=150, y=130)

janela.mainloop()

In bank ( usuarios.py ) you do not need return at the end, since nothing is being returned:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
""""""
import sqlite3


def cria_bd(email, senha):
    conn = sqlite3.connect('usuarios.db')
    bd = conn.cursor()
    bd.execute("""CREATE TABLE IF NOT EXISTS Cadastro (
        id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
        Email TEXT NOT NULL,
        Senha TEXT NOT NULL);""")

    bd.execute("""INSERT INTO Cadastro (Email, Senha) VALUES (?,?);""", (email, senha))
    conn.commit()
    conn.close()

I only care with conn.close() at the end of the method, because if you are going to perform more operations with the database the connection will be closed.

    
21.11.2018 / 16:10