MySQLdb._exceptions.OperationalError('1046,未选择数据库')-烧瓶到mySQL连接错误

时间:2019-02-27 22:09:23

标签: python mysql flask flask-mysql

我花了很多时间试图解决这个问题,但我迷路了。

我正在尝试制作一个小型Web应用程序,观看一些教程...

  

我不断收到此错误:MySQLdb._exceptions.OperationalError('1046,未选择数据库')

这是我的代码:

# app.py
from flask import Flask, render_template, flash, redirect, url_for, session, logging, request
# from flask_sqlalchemy import SQLAlchemy
# from data import Articles
from data import Articles
from flask_mysqldb import MySQL
from wtforms import Form, StringField, TextAreaField, PasswordField, validators
from passlib.hash import sha256_crypt

app = Flask(__name__)
# app.config['SECRET_KEY'] = 'xxxxxxxxxx'

    # Config MySQL
    app.config['MySQL_HOST'] = 'localhost'
    app.config['MySQL_USER'] = 'root'
    app.config['MySQL_PASSWORD'] = ''
    app.config['MySQL_DB'] = 'myflaskapp'
    app.config['MySQL_CURSORCLASS'] = 'DictCursor'
    # Init MySQL

    mysql = MySQL(app)

    # Setting articles
    Articles = Articles()


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


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


@app.route('/articles')
def articles():
    return render_template('articles.html', articles=Articles)


@app.route('/article/<string:id>/')
def article(id):
    return render_template('article.html', id=id)


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))

        # Creat Cursor
        cur = mysql.connection.cursor()
        # Execute Query
        cur.execute("INSERT INTO users(name, email, username, password) VALUES(%s, %s, %s, %s)",
                    (name, email, username, password))
        # Commit to DB
        mysql.connection.commit()
        # close connection
        cur.close()

        # Message to user once registered
        flash('You are now registered and can login, thank you', 'success')
        return redirect(url_for('index'))
    return render_template('register.html', form=form)


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

如果我对MYSQL配置问题发表评论,我没有收到密码被拒绝的错误,那么我很困惑,在安装MySQL期间是否搞砸了?

此外,我已经使用show database检查了我的数据库是否存在;我已经访问了myflaskapp中的用户表,并且还进行了phpmyadmin的检查。

1 个答案:

答案 0 :(得分:1)

我解决了自己的问题。

1)app.config ['MySQL_HOST'] ='localhost'-> MySQL需要替换为'MYSQL'

  1. return redirect(url_for('index'))->索引需要替换为'register'

简单的拼写错误...