简体   繁体   English

有没有一种方法可以检查记录中的特定字段,使用 SQLite 中的现有 python 变量

[英]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.

相关问题 无法使用tkinter作为gui在sqlite数据库中搜索记录来输入我要搜索的记录的名称(Python) - Can't search for a record in sqlite database using tkinter as a gui to enter the name of the record I am searching for (Python) 在现有SQLite记录中更新BLOB字段的语法? - syntax to UPDATE a BLOB field in an existing SQLite record? 如何在Amazon Route 53中使用Python Boto编辑现有的A记录 - How can i edit existing A record using Python Boto in Amazon Route 53 有没有办法检查特定程序是否正在运行(使用 Python)? - Is there a way to check if a specific program is running (using Python)? 有没有办法检查特定进程是否正在使用 Python 运行? - Is there a way to check if a specific process is running using Python? Python SQLite3 If字段==变量 - Python SQLite3 If field == variable 如何检查域/子域是否具有特定的A记录? - How can I check if a domain/subdomain has a specific A record? 如何检查python中的特定字符串? - How can I check for specific strings in python? 如何使用Pandas将python Web抓取数据导出到现有excel文件中的特定工作表? - How can I export my python web scrape data to a specific sheet in an existing excel file using pandas? 如何使用 python Z3A43B4F88325D2FA5AZC22 在特定单元格中获取 dataframe 以覆盖现有 excel? - How can i get a dataframe to overwrite an existing excel in a specific cell using python pandas?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM