Search code examples
pythonsqliterfid

Retrieve data from sqlite3 database with Python


So I am trying to retrieve all the users IDs from my sqlite3 database. I have a tkinter gui with a check-in Buttonwhen the user clicks the button they are prompted to scan their tag(rfid). I am trying to compare the tag id once the tag is scanned to all of the users Uids in the database and check if the two match. If they do match I am trying to print User Verified if the tag does not match one of the UIDs in the table I am trying to print No user exist,Denied

from tkinter import *
import sqlite3 as sql
import RPi.GPIO as GPIO
from mfrc522 import SimpleMFRC522

reader = SimpleMFRC522()

global r
global uid_tag
def chk():
    Database = sql.connect('MedaDataBase.db')
    # cursor
    c= Database.cursor()

    #Query uid database
    c.execute("SELECT*, userID FROM Users ")

    #Fetch all uids from Database
    r= c.fetchall()

    if r:
        #Getting user uid from tag 
        id,uid_tag = reader.read()
        print(uid_tag)
        checker()

def checker():
    global uid_tag
    global r
    for x in r:
        #If userId from tag is in the database 
        if uid_tag == x:

            print("User Verified")
        else:
            print("Denied")    
            Database.commit()
            Database.close()

Solution

  • in chk(), r is not defined as global, so you are reading to a local variable and the global variable is still uninitialized.