Python MySQLdb.\u异常。编程错误

Python MySQLdb.\u异常。编程错误,python,mysql,mysql-python,Python,Mysql,Mysql Python,这是我的源代码: @app.route('/pythonlogin/register', methods=['GET', 'POST']) def register(): # Output message if something goes wrong... msg = '' # Check if "username", "password" and "email" POST requests exist (user submitted form) if requ

这是我的源代码:

@app.route('/pythonlogin/register', methods=['GET', 'POST'])
def register():
    # Output message if something goes wrong...
    msg = ''
    # Check if "username", "password" and "email" POST requests exist (user submitted form)
    if request.method == 'POST' and 'username' in request.form and 'password' in request.form and 'email' in request.form:
        # Create variables for easy access
        username = request.form['username']
        password = request.form['password']
        email = request.form['email']
                # Check if account exists using MySQL
        cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
        cursor.execute("SELECT * FROM accounts WHERE username = %s", (username))
        account = cursor.fetchone()
        # If account exists show error and validation checks
        if account:
            msg = 'Account already exists!'
        elif not re.match(r'[^@]+@[^@]+\.[^@]+', email):
            msg = 'Invalid email address!'
        elif not re.match(r'[A-Za-z0-9]+', username):
            msg = 'Username must contain only characters and numbers!'
        elif not username or not password or not email:
            msg = 'Please fill out the form!'
        else:
            # Account doesnt exists and the form data is valid, now insert new account into accounts table
            cursor.execute("INSERT INTO accounts VALUES (NULL, %s, %s, %s)", (username, password, email))
            mysql.connection.commit()
            msg = 'You have successfully registered!'
    elif request.method == 'POST':
        # Form is empty... (no POST data)
        msg = 'Please fill out the form!'
    # Show registration form with message (if any)
    return render_template('register.html', msg=msg)

我无法理解为什么会出现此错误,我的登录工作正常,但我的注册我有一个小问题,我在myswl服务器上运行感谢您的时间

您的第一个查询有一个小问题

cursor.execute(“从用户名=%s的帐户中选择*,(用户名))

这样做的目的是解压作为第二个参数传递的值,并将其放入查询中,通常使用元组完成。现在这似乎是你想要的,但是这里缺少了一个次要但非常重要的部分。一个值为的元组需要一个尾随逗号,即
(用户名),
,否则它只是字符串周围的括号。因此,您的字符串实际上正在解压,每个字符都将作为参数传递给您的查询

TL;博士

您需要在查询中添加尾随逗号来传递元组,而不是字符串作为参数


cursor.execute(“SELECT*FROM accounts WHERE username=%s”,(username,)

请添加完整的错误消息。MySQLdb.\u exceptions.ProgrammingError MySQLdb.\u exceptions.ProgrammingError:在字节格式化过程中未转换所有参数这是我的错误,这是一个屏幕截图