使用 Python 从 sqlite3 数据库中检索数据
Retrieve data from sqlite3 database with Python
所以我试图从我的 sqlite3 数据库中检索所有用户 ID。我有一个带有签入的 tkinter gui Button
当用户单击按钮时,系统会提示他们扫描他们的标签 (rfid)。一旦标签被扫描到数据库中的所有用户 Uid,我就会尝试比较标签 ID,并检查两者是否匹配。如果它们确实匹配,我将尝试打印 User Verified 如果标签与 table 中的 UID 之一不匹配,我将尝试打印 No user存在,拒绝
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()
在 chk() 中,r 没有被定义为全局变量,所以你读取的是局部变量,而全局变量仍未初始化。
所以我试图从我的 sqlite3 数据库中检索所有用户 ID。我有一个带有签入的 tkinter gui Button
当用户单击按钮时,系统会提示他们扫描他们的标签 (rfid)。一旦标签被扫描到数据库中的所有用户 Uid,我就会尝试比较标签 ID,并检查两者是否匹配。如果它们确实匹配,我将尝试打印 User Verified 如果标签与 table 中的 UID 之一不匹配,我将尝试打印 No user存在,拒绝
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()
在 chk() 中,r 没有被定义为全局变量,所以你读取的是局部变量,而全局变量仍未初始化。