3

I have spent so much time trying to figure this out and I am lost.

I am trying to make a small web app, watching some tutorials...

I keep getting this error: MySQLdb._exceptions.OperationalError ('1046, No database selected')

This is my code:

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

If I comment of MYSQL config things I don't get a password denied error, so I am very confused, did I mess up during the MYSQL install?

Further, I have checked my DB exists by using show database; I have accessed the user table inside myflaskapp and also went on phpmyadmin to check.

KingAlmond
  • 93
  • 2
  • 8
  • Welcome to StackOverflow! Please clarify: you wrote **"OperationalError ('1046, No database selected')"** but then **"I don't get a password denied error"** - so which error you get? – Alex Yu Feb 27 '19 at 22:15
  • its the 1046, no database selected. I was just saying I don't get a password denied @ user if I comment out the password config which I find weird because I should get that if it is commented out. – KingAlmond Feb 27 '19 at 22:21

1 Answers1

3

I solved my own problems.

1) app.config['MySQL_HOST'] = 'localhost' --> MySQL needs to be replaced with 'MYSQL'

  1. return redirect(url_for('index')) --> index needs to be replaced with 'register'

simple spelling mistakes...

KingAlmond
  • 93
  • 2
  • 8