[英]Is there a way I can check a specific field in a record, using an existing python variable in SQLite
Sorry if this question seems convoluted or is easilt googlable, I was struggling to find anything particularly helpful.抱歉,如果这个问题看起来很复杂或者很容易用谷歌搜索,我正在努力寻找任何特别有用的东西。 Essentially, at the beginning of my python program I have a login which checks the database to see if the username and password match:
本质上,在我的 python 程序开始时,我有一个登录名,用于检查数据库以查看用户名和密码是否匹配:
existingAccount = input("Do you already have an account? (y/n)")
if existingAccount == "y":
username = input("Please enter your username: ")
password = input("Please enter your password:")
find_user = ("SELECT * FROM Users WHERE Username = ? AND Password = ?")
c.execute(find_user, [(username), (password)])
results = c.fetchall()
This creates the variable results which I want to use as a way of checking the active user for creating an order and checking their permission level which is stored in the Users table.这将创建变量结果,我想将其用作检查活动用户以创建订单并检查其存储在用户表中的权限级别的方法。
I am going to write a function which will allow someone to add a new product to the products table but i only want users with the permission level admin to be able to do it.我将编写一个 function 允许某人将新产品添加到产品表中,但我只希望具有权限级别管理员的用户能够执行此操作。
Is there a way I can do some sort of if statement using the results variable to say "if results contains userPermissionLevel Admin then continue, if not print "permission level inadequate"有没有办法我可以使用结果变量执行某种 if 语句来表示“如果结果包含 userPermissionLevel Admin 然后继续,如果不打印”权限级别不足“
Thanks and sorry again if this is an easy question.如果这是一个简单的问题,再次感谢和抱歉。
Considering that results will have only one row (which is the normal) you can just check the results[0] item and the respective position of your userPermissionLevel column.考虑到结果只有一行(这是正常的),您只需检查 results[0] 项和您的 userPermissionLevel 列的相应 position 即可。 For example if it's your 5th column you can just check with the following:
例如,如果它是您的第 5 列,您可以检查以下内容:
if results[0][5]=='Admin':
do_some_stuff
else:
print('permission level inadequate')
You can also check the specific column by name, if you have run this line after the creation of your sqlite3 connection:如果您在创建 sqlite3 连接后运行此行,您还可以按名称检查特定列:
conn.row_factory = sqlite3.Row
In that case, you can change the aboce code to this, using the column name:在这种情况下,您可以使用列名将 aboce 代码更改为此:
if results[0]['userPermissionLevel']=='Admin':
do_some_stuff
else:
print('permission level inadequate')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.