简体   繁体   English

使用 python 和 sqlite3 检查用户名是否在数据库中

[英]Check if a username is in a database using python and sqlite3

I'm trying to check if a username is in a database using python and SQLite3, here's my code我正在尝试使用 python 和 SQLite3 检查用户名是否在数据库中,这是我的代码

name = request.form.get("username")
namecheck = db.execute("SELECT * FROM users WHERE username=?", (name))
if name in namecheck:
    return apology("Username already exists")

Sorry, I'm still pretty new to this, why isn't this working?抱歉,我对此还是很陌生,为什么这不起作用?

The pattern you are using is wrong here.您使用的模式在这里是错误的。 Most typically you should be calling db.execute() as a void function.通常情况下,您应该将db.execute()作为 void function 调用。 Then, you should be iterating the db object to get the records.然后,您应该迭代db object 以获取记录。 That being said, I would use an exists query here:话虽如此,我会在这里使用存在查询:

name = request.form.get("username")
sql = 'SELECT EXISTS (SELECT 1 FROM users WHERE username = ?)'
db.execute(sql, (name,))
if db.fetchone()[0]:
    return apology("Username already exists")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM