Flask Mysql Connector问题

时间:2017-12-28 19:52:26

标签: python mysql flask

在烧瓶上制作一个简单的登录/注册应用程序,我遇到了这个问题?

我在这里做错了什么?注册表格完美无缺。登录部分不起作用 我该如何解决呢? 我必须这样做才能用python

的mysql.connector替换flask-mysqldb

我在登录提交时收到此错误:

mysql.connector.errors.InternalError

mysql.connector.errors.InternalError:找到未读结果

为登录创建一个新的dB连接,给我这个错误

builtins.TypeError

TypeError:'>' ' NoneType'的实例之间不支持和' int'

app.py是:

from flask import Flask, render_template, flash, redirect, url_for, session, request, logging
from wtforms import Form, StringField, TextAreaField, PasswordField, validators
from passlib.hash import sha256_crypt
import mysql.connector

app = Flask(__name__)

app.secret_key='secretkey'


#Config MySQL
cnx = mysql.connector.connect(user='jay', password='jay',
                              host='127.0.0.1',
                              database='usertable')
#cnx.close()

@app.route('/')
def index():
    return render_template('home.html')


class RegisterForm(Form):
    name = StringField('Name', [validators.Length(min=1, max=50)])
    username = StringField('Username', [validators.Length(min=4, max=25)])
    email = StringField('Email', [validators.Length(min=6, max=50)])
    password = PasswordField('Password', [
        validators.DataRequired(),
        validators.EqualTo('confirm', message='Passwords do not match')
        ])
    confirm = PasswordField('Confirm Password')



@app.route('/register', methods=['GET','POST'])
def register():
    form = RegisterForm(request.form)
    if request.method == 'POST' and form.validate():
        name = form.name.data
        email = form.email.data
        username = form.username.data
        password = sha256_crypt.encrypt(str(form.password.data))
        #connect to db
        #create Cursor
        cur = cnx.cursor()
        cur.execute("INSERT INTO users(name, email, username, password) VALUES(%s, %s, %s, %s)", (name, email, username, password))
        #commit 2 DB
        cnx.commit()
        #close connection
        cur.close()
        cnx.close()
        flash('You are now registered and can log in', 'success')

        return redirect(url_for('login'))


    return render_template('register.html', form=form)


#USER login CONFIGURaTION
@app.route('/login', methods=['GET', 'POST'])
def login():
    if request.method == 'POST':
        #get form fields
        username = request.form['username']
        password_candidate = request.form['password']
        #connect to db
        conn = mysql.connector.connect(user='jay', database='table_users')
        #cursor creation
        cur = cnx.cursor(dictionary=True, buffering=True)
        #get user by username
        result = cur.execute("SELECT * FROM users Where username = %s", [username])

        if cur:
            #get stored hash
            data = cur.fetchone()

            password = data['password']
            #compare passwords
            if sha256_crypt.verify(password_candidate, password):
                app.logger.info('Password MATCHED')
            else:
                app.logger.info('Password not MATCHED')
        else:
            app.logger.info('No USER')
        cur.close()
        cnx.close()


    return render_template('signin.html')




if __name__ == '__main__':
    app.run(debug=True)

1 个答案:

答案 0 :(得分:0)

不需要关闭连接并重新打开,您可以改为使用它:

your_database.get_conn()