I am trying to pass the inputted value from one function to another function. How it works is the user click the Tkinter button which runs the chk():
function. Once the button is click the user will have to scan their tag (rfig tag) which will read the user's tagID
and give the idTag
variable a value. When the idTag
is returned the dataCheck():
function will be called and check if the idTag
value match one of the values in the userID column of my sqlite3 database.
My issue is I keep getting the Error : name 'idTag' is not defined
The command reader.read()
acts like a input function because the user actually has to scan (or input) their tag before they can continue. I think the problem is the function is being call as soon as the button is being click which is causing the idTag
variable to still be empty due to the user not having inputted the value yet. Any thoughs?
from tkinter import *
import sqlite3 as sql
import RPi.GPIO as GPIO
from mfrc522 import SimpleMFRC522
reader = SimpleMFRC522()
global idTag
# Tkinter button click Command
def chk():
# Function that handels getting User ID
def tagScanner(idTag):
#Get user id value
tagID = reader.read()
#If tag is scanned
if tagID:
idTag= (tagID,)
return idTag
#call Database function to check returned idTag
dataCheck(idTag)
# Function handels SQLite3 Database
def dataCheck(idTag):
Database = sql.connect('MedaDataBase.db')
# cursor
c= Database.cursor()
#Check if the idTag maches a value in the userID column of sqlite DB
query = 'SELECT userID FROM Users "WHERE" userID = "idTag"'
c.execute(query)
c.fetchone()
if query == idTag: #if userID is returned
print('User Verified')
else:
print('Denied')
Database.close()
#Call scanning function
tagScanner(idTag)
Several issues here
First, get rid of global idTag
as I think that's just creating scope issues. You don't need a global variable.
tagScanner()
does not make use of its only input argument, so get rid of it. You're getting the id from reader
, so this function does not need another input.
You're comparing the input idTag
in dateCheck(idTag)
to your query string instead of whatever's returned by the query, that's probably not your intention.
When the interpreter reaches a return
in a function, the function exits. The last line of tagScanner()
will never execute, because it returns right before that. Try calling dataCheck(idTag)
before you return idTag
Your sqlite query is always querying for "idTag", e.g. the string of characters "idTag" rather than the value that you read in and assigned to the variable idTag
. Use ?
to designate that you want to provide a value during the query, see the docs here:
https://docs.python.org/2/library/sqlite3.html
Putting it all together:
from tkinter import *
import sqlite3 as sql
import RPi.GPIO as GPIO
from mfrc522 import SimpleMFRC522
reader = SimpleMFRC522()
# Tkinter button click Command
def chk():
# Function that handels getting User ID
def tagScanner():
# Get user id value
idTag = reader.read()
# If tag is scanned
if idTag:
# call Database function to check returned idTag
dataCheck(idTag)
return idTag
# Function handles SQLite3 Database
def dataCheck(idTag):
Database = sql.connect('MedaDataBase.db')
# cursor
c = Database.cursor()
# Check if the idTag maches a value in the userID column of sqlite DB
query = 'SELECT userID FROM Users WHERE userID = ?'
c.execute(query, (idTag,))
row_returned = c.fetchone()
if row_returned is not None:
# Found a matching row
print('User Verified')
else:
print('Denied')
Database.close()
# Call scanning function
tagScanner()