Skip to content
Advertisement

How to get different values from different tables in Treeview / Tkinter (SQLite)?

I’ve been working with Tkinter and I’m trying to get different columns from different tables in my Treeview, I’m using SQLite as database, in my first table called registers i got different columns idcard, name, surname, cellphone, address, email, etc in my second table named attendance I use to store my entries attendances by ID number I mean I got two entries cardin = entry1 and cardout = entry1 so if one of my entries match with my registers table idcard, it automatically store idcard, timein, timeout, date in my attendance table till that it works so good, but I canĀ“t get the names, surnames columns from my first table registers and store them in my second table attendace same as my idcard, timein, timeout, date (the idcard, names, surnames must match from the first table), I was trying to solve it but I falied, I really have no idea how to do it, any help thanls in advance. have a good day. Here is my code:

def botoningreso():

    time = datetime.now().strftime("%I:%M%p")
    date = datetime.now().strftime("%d/%m/%Y")

    conn = sqlite3.connect('database.db')
    c = conn.cursor()

    cardin = entry1.get()
    cardout = entry2.get()

    c.execute('SELECT * FROM registers WHERE idcard = ? OR idcard = ?', (cardin, cardout))

    if c.fetchall():
        messagebox.showinfo(title='ID Registered', message='Registered Succefully')
        c.execute("INSERT INTO attendance (timein, idcard, date) VALUES (?, ?, ?)", (time, cardin, date)) #Here i also wanto to save *names and surnames* from the first table *registers* that match with the idcard column.
        conn.commit()

    else:
        messagebox.showerror(tittle=None, message='Wrong ID card')

    c.close() 

Advertisement

Answer

Assign a variable to fetched data and then index it and proceed:

data = c.fetchall()

if data:
    username = data[0][1] # Access username from fetched list
    surname = data[0][2] # Access surname

    messagebox.showinfo(title='ID Registered', message='Registered Succefully')
    c.execute("INSERT INTO attendance (timein,idcard,date,user,sur) VALUES (?,?,?,?,?)", (time,cardin,date,username,surname)) 
    conn.commit()

It is important to assign variable to c.fetchall() as it behaves like a generator object and loses its items once its been used.

Advertisement