python - 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()
解决方案
推荐阅读
- tensorflow - CNN 训练不稳定
- django - Django让超级用户在模型类中使用
- javascript - 常量重新声明 Javascript
- c# - Docker 设置中的 .Net Core 3.1 MSSQL 后端:Docker-compose up 产生连接失败
- postgresql - 计算 jsonb 列,其中数据就像 PostgreSQL 12 中的刺一样
- javascript - Mongoose - 我如何测试我收藏的特定文档是否设置了所有字段
- ruby-on-rails - load_and_authorize_resource 是授权控制器中的每一个动作,还是只授权 RESTful 动作?
- javascript - 如何根据控制器的响应在视图中显示完全不同的模态?
- flutter - 这两个构造函数有什么区别?
- ruby-on-rails - Rails 在嵌套包含的 where 子句中包含所有“父”记录