How to rescue sqlite3 data with python?

Asked

Viewed 36 times

0

I have an application in Tkinter (Basic registration that every apprentice does) where I saved the data in sqlite3, but I’m having difficulties in recovering this data. What I need exactly is:

  • That he returns me inside a combobox the name of each registered person

  • And also all the data of each user However I can not at all get this information out of the list, I tried every possible loop, but returns me error. Below is a print of the program and the code relevant to the database:

  • any help will be welcome, I have tried to run the various examples of documentation and nothing, and I need to finish this work. I leave my thanks to those who help me...

def consulta_BD(self):
    conexao = sqlite3.connect("saveData.db")
    c = conexao.row_factory = sqlite3.Row
    c = conexao.cursor()
    c.execute("SELECT * FROM dadosDoUsuario")
    r = c.fetchall()
    consulta = [dict(row) for row in r]
    return consulta
    c.close()
    conexao.close()

def commandButtonSalvar(self):
    conexao = sqlite3.connect("saveData.db")
    c = conexao.cursor()
    # id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
    c.execute(
        """
    CREATE TABLE IF NOT EXISTS dadosDoUsuario (
    nome TEXT NOT NULL,
    data_nasc TEXT,
    sexo TEXT,
    telefone TEXT,
    celular TEXT,
    rg TEXT,
    cpf TEXT,
    endereco TEXT,
    email TEXT);
        """
    )
    c.execute(
        "INSERT INTO dadosDoUsuario VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)",
        (
            self.entry_nome.get(),
            self.dateEntry.get(),
            self.combobox_sexo.get(),
            self.entryTelef.get(),
            self.entryCelular.get(),
            self.entryRG.get(),
            self.entryCPF.get(),
            self.entryEnd.get("1.0", "end"),
            self.entryEmail.get(),
        ),
    )
    conexao.commit()
    conexao.close()
    self.mensagem('info', 'Atenção!', 'Cadastro salvo!')
    self.clear()

inserir a descrição da imagem aqui

1 answer

2

After a lot of research and several tests I changed the code section below that made everything work right.

self.nomes = []

def consulta_BD(self):
        conexao = sqlite3.connect("saveData.db")
        cursor = conexao.cursor()
        cursor.execute("SELECT * FROM dadosDoUsuario ORDER BY nome")
        r = cursor.fetchall()

        for n in r:
            self.nomes.append(n[1].title())
        return self.nomes

I leave here recorded in case someone needs this solution, anyway thank you all...

Browser other questions tagged

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