首页 > 解决方案 > Python SQLite3 问题搜索表

问题描述

我有一个 Python 函数,它应该检查我的 SQLite 表中是否存在值。我有一个名为的表Users,它存储的值userid是整数和字母的随机字符串。例如, auserid看起来像这样f75fsf3dbg9g。在def chk():用户输入他们的uid然后uid_chk():调用该函数以检查该uid值是否等于userid存储在表中的值之一。

我的问题出cur.execute(query,tagValue)在函数行中uid_chk()。它抛出错误sqlite3.programmingError:提供的绑定数量不正确。当前语句使用 1 函数没有返回任何内容,uid_chk()只是打印出来None

Users打印出表格时供参考,如下所示:[('hk12lkj14vh',),('bn465i32',),('d8j4k0fl',),('k4i9b3k0s2',),('rk2nk50sp26',),('rk3ji9al23',),]

import RPi.GPIO as GPIO
import sqlite3 as sql
from mfrc522 import SimpleMFRC522
reader = SimpleMFRC522()

def chk():
    print("Scan tag")
    try:
        uid_tag = reader.read()
        #If the user has inputed value (uid)
        if uid_tag :
            #Var for inputed userid 
            tagValue = uid_tag[1]
            print(tagValue)
            uid_chk(tagValue)
            return tagValue
            GPIO.cleanup()
    except ValueError:
        print('Something went wrong')
       
    

def uid_chk(tagValue):
    Database = sql.connect('test.db')
    # cursor
    cur = Database.cursor()
    # Check if the tagValue maches a value in the userid column of sqlite DB
    query = 'SELECT userid FROM Users WHERE userid = ?'
    cur.execute(query, tagValue)
    row_returned = cur.fetchone()
    print(row_returned)
    if row_returned == tagValue:
        print('User Verified')
    else:
        print('Something is Wrong')
        Database.close()

chk()

标签: pythonsqlite

解决方案


推荐阅读