嘿,任何人都可以告訴我,我在我的代碼中做錯了什麼?我想檢查用戶名是否已經在數據庫中。FLASK SQlite Python - 檢查用戶名是否已經在數據庫中
整個路線。它不會給我任何錯誤,但也不會填補我的數據庫。
@app.route('/regist', methods=['GET', 'POST'])
def regist():
if request.method == "POST":
with sql.connect("database.db") as con:
cur = con.cursor()
try:
# flash("register attempted")
username = request.form['username']
password = request.form['password']
passwordencr = request.form['password']
email = request.form['email']
x = cur.execute("SELECT * FROM users WHERE name = ?", (username))
if int(len(x)) > 0:
flash("That username is already taken, please choose another")
return render_template('register.html')
else:
cur.execute("INSERT INTO users (name,password,email) VALUES (?,?,?)",(username,passwordencr,email))
con.commit()
flash ("Successfully registrated")
except:
con.rollback()
msg = "error in insert operation"
finally:
session['logged_in'] = True
session['username'] = username
gc.collect()
msg = Message('Hello', sender='[email protected]', recipients=[email])
msg.body = "your username for ak047 is: %s and your password is %s" %(username,password)
mail.send(msg)
return render_template("home.html", msg=msg)
con.close()
gc.collect()
我覺得你的代碼肯定是錯誤的,你不應該使用,除非你有一個毯子,如果你沒有,你會看到你的錯誤 –
你並不需要調用'INT(LEN(X) )'。 'len'返回一個整數。你也不需要比較0的長度。'如果len(x)> 0'和'if x'相同。 – dirn