简体   繁体   中英

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. Second, you are not checking existence correctly. Third you do not need an else:pass that is assumed.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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