Sqlite操纵数据库中的字符串

时间:2012-11-13 17:39:17

标签: python html sqlite

在我的登录方法中,我试图检查数据库中的密码是否与用户提交的密码相对应。我使用这段代码:

@app.route('/login', methods=['GET', 'POST'])
def login():
error = None
db = get_db()
if request.method == 'POST':
    cur = db.execute('select password from users where username = ?', (request.form['username'], ))
    password = cur.fetchone()
    print password
    print request.form['password']
    if request.form['password'] != password:
        error = 'Invalid username/password combination'
    else:
        session['logged_in'] = True
        flash('You were logged in')
        return redirect(url_for('show_entries'))
return render_template('login.html', error=error)

例如,当密码是默认密码时,这就是打印命令显示的内容:

print password : (u'default',)
print request.form['password'] : default

所以他们确实不平等。有谁知道如何解决这个问题?

1 个答案:

答案 0 :(得分:4)

数据库返回的每一行都是一列列。您检索了一行(.fetchone()),该行是一个元组(只有password列)。只需使用它的索引从元组中取出 out

password = cur.fetchone()[0]