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

Asked

Viewed 830 times

0

I am studying, on my own Sqlite, I already bought some courses, but I need a help.

Objective: I would like to save the user typed 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 the save button, three columns must be saved in the database (auto-increment id, email, password)

The code below shows only the window code, Below, you will have the code of the module users, where I put the code Sqlite.

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 the 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

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 its code of example 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()

On the bench (usuarios.py) you don’t need the 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 deal only with the conn.close() at the end of the method, this is because if you are going to carry out more operations with the bank the connection will be closed.

  • Caro @Renato Cruz, thank you for your attention! Thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.