1

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() 
12
  • In short, you want to get the username and surname from first table that has the id you insert into second table? Commented Mar 1, 2021 at 20:34
  • @CoolCloud Yes man, untill now I just got idcard, timein, timeout and date in my second table once I enter a right idcard stored in my first data table. Commented Mar 1, 2021 at 20:43
  • What is the output of print(c.fetchall() before the if. Commented Mar 1, 2021 at 20:44
  • @CoolCloud I tried to print(c.fetchall() before if but it didn't give me any value Commented Mar 1, 2021 at 20:49
  • Doesn't that mean your query is not returning any result and is wrong? Commented Mar 1, 2021 at 20:51

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.