python - 错误 :not all arguments converted during string formatting

标签 python mysql mysql-python

我有这个注册页面,当我提交表单时出现此错误:并非所有参数都在字符串格式化期间转换

class RegistrationForm(Form):
email = StringField('Email address')
password = PasswordField('password')
name = StringField('Name')

@app.route('/register/', methods=['GET', 'POST'])
def register():
try:
    form = RegistrationForm(request.form)
    if request.method == 'POST':
        email = form.email.data
        password = sha256_crypt.encrypt((str(form.password.data)))
        con = connection()
        cur=con.cursor()
        x = cur.execute("SELECT * FROM user WHERE username = (%s)",(thwart(email)))
        if int(x) > 0:
            return render_template('register.html',form=form)
        else:
            cur.execute("INSERT INTO user (username,password,name) VALUES (%s,%s,%s);",(thwart(email),thwart(password),thwart(name),))
            con.commit()
            cur.close()
            con.close()
            return redirect(url_for('dashboard'))

    return render_template('register.html', form=form)
except Exception as e:
    return str(e)

最佳答案

x = cur.execute("SELECT * FROM user WHERE username = (%s)",(thwart(email)))

execute 的第二个参数应该是一个元组,你缺少一个 ,:

cur.execute("SELECT * FROM user WHERE username = (%s)",(thwart(email),))

我猜你也不需要 %s 周围的 () 但这取决于你的表的实际外观。

进一步说明:

('str') 将求值为字符串 'str',而不是包含它的元组。

为了创建一个元组,您必须包含一个逗号:('str',)

关于python - 错误 :not all arguments converted during string formatting,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38474576/

相关文章:

php - 用于交 key Web 应用程序的 Django 或 CodeIgniter

mysql - Laravel:whereNotIn 没有按预期工作

mysql - 触发器不起作用

来自 mysqldb 的 Python 列表格式

python - MySQLClient 安装错误 : "raise Exception("Wrong MySQL configuration: maybe https://bugs. mysql.com/bug.php?id"

python - 加快大量 mysql 更新查询

python - 如何使用 python 在 csv 文件中删除 None 行?

python - 展开 numpy 矩阵

php - MySQL 从按项目分组的表中选择用户列表,按小时数降序列出

python - 删除行后 commit() 未提交 mysql 表