简体   繁体   English

检查数据库中用户的存在(python / sqlite3)

[英]check the presence of a user in a database (python / sqlite3)

def dashboard():
    
    p = request.form['Pseudo']
    if cur.execute("SELECT * FROM pseudo WHERE user = ?", (p,)) == 0:
        
        cur.execute("INSERT INTO pseudo (user, score, win, coup) VALUES (?,?,?,?)", (p,0,0,0))
        conn.commit()
        
    else:
        pass
    
    return render_template("index.html",data=liste, Pseudo = p)

Hello, my program doesn't display any error, it's a project for classes and I've been stuck for several hours.您好,我的程序没有显示任何错误,这是一个课程项目,我已经卡了几个小时。 However I try to see if the user is already registered in the database and if so, register him.但是,我尝试查看用户是否已在数据库中注册,如果是,请注册他。 However the program acts as if the user is still present in the database.但是,该程序的行为就像用户仍然存在于数据库中一样。

There are a couple of issues with your design.你的设计有几个问题。 First, you need to fetch from the executed SQL.首先,您需要从执行的 SQL 中获取。 Second, you are not checking existence correctly.其次,您没有正确检查存在。 Third you do not need an else:pass that is assumed.第三,您不需要假定的else:pass

def dashboard():
    
    p = request.form['Pseudo']
    user_check = cur.execute("SELECT * FROM pseudo WHERE user = ?", (p,)).fetchone()
    if user_check:    
        cur.execute("INSERT INTO pseudo (user, score, win, coup) VALUES (?,?,?,?)", (p,0,0,0))
        conn.commit()
                
    return render_template("index.html",data=liste, Pseudo = p)

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

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