Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python MySQLdb._exceptions.OperationalError(';1046,未选择任何数据库';)-烧瓶到mySQL连接错误_Python_Mysql_Flask_Flask Mysql - Fatal编程技术网

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

Python MySQLdb._exceptions.OperationalError(';1046,未选择任何数据库';)-烧瓶到mySQL连接错误,python,mysql,flask,flask-mysql,Python,Mysql,Flask,Flask Mysql,我花了这么多时间试图弄明白这一点,我迷路了 我正在尝试制作一个小的网络应用程序,看一些教程 我一直收到这个错误:MySQLdb.\u exceptions.OperationalError('1046,未选择数据库') 这是我的代码: # app.py from flask import Flask, render_template, flash, redirect, url_for, session, logging, request # from flask_sqlalchemy impor

我花了这么多时间试图弄明白这一点,我迷路了

我正在尝试制作一个小的网络应用程序,看一些教程

我一直收到这个错误:MySQLdb.\u 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)
#app.py
从flask导入flask、呈现模板、flash、重定向、url、会话、日志、请求
#从flask_sqlalchemy导入sqlalchemy
#从数据导入文章
从数据导入文章
从flask_mysqldb导入MySQL
从wtforms导入表单、StringField、TextAreaField、PasswordField、验证器
从passlib.hash导入sha256_crypt
app=烧瓶(名称)
#app.config['SECRET_KEY']='xxxxxxxxx'
#配置MySQL
app.config['MySQL\u HOST']='localhost'
app.config['MySQL\u USER']='root'
app.config['MySQL_PASSWORD']=''
app.config['MySQL_DB']='myflaskapp'
app.config['MySQL\u CURSORCLASS']='DictCursor'
#初始化MySQL
mysql=mysql(应用程序)
#定稿
文章=文章()
@应用程序路径(“/”)
def home():
返回渲染模板('home.html')
@app.route(“/about”)
def about():
返回render_模板('about.html')
@app.route(“/articles”)
def articles():
返回呈现模板('articles.html',articles=articles)
@app.route(“/article/”)
def物品(id):
返回呈现模板('article.html',id=id)
注册类别(表格):
name=StringField('name',[validators.Length(最小值=1,最大值=50)])
username=StringField('username',[validators.Length(最小=4,最大=25)])
email=StringField('email',[validators.Length(最小=6,最大=50)])
password=PasswordField('password',[validators.DataRequired(),
验证器(
“确认”,消息=“密码不匹配!”)
])
确认=密码字段(“确认密码”)
@app.route('/register',methods=['GET','POST'])
def寄存器():
form=RegisterForm(request.form)
如果request.method==“POST”和form.validate():
name=form.name.data
电子邮件=form.email.data
用户名=form.username.data
password=sha256_crypt.encrypt(str(form.password.data))
#创建光标
cur=mysql.connection.cursor()
#执行查询
cur.execute(“插入用户(名称、电子邮件、用户名、密码)值(%s,%s,%s,%s)”,
(姓名、电子邮件、用户名、密码))
#提交给数据库
mysql.connection.commit()
#密切联系
当前关闭()
#注册后发送给用户的消息
flash('您现在已注册,可以登录,谢谢,'成功')
返回重定向(url_for('index'))
返回呈现模板('register.html',form=form)
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
app.run(debug=True)
如果我评论MYSQL配置的东西,我不会得到一个密码拒绝错误,所以我很困惑,我是不是在MYSQL安装过程中搞砸了


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

我解决了自己的问题

1) app.config['MySQL\u HOST']='localhost'->MySQL需要替换为'MySQL'

  • 返回重定向(url_for('index'))-->索引需要替换为“register”

  • 简单的拼写错误…

    欢迎使用StackOverflow!请澄清:您写了“OperationalError('1046,未选择数据库')”但又写了“我没有收到密码拒绝错误”-那么您收到的是哪个错误?是1046,未选择数据库。我只是说,如果我注释掉密码配置,我不会得到一个被拒绝的@user密码,我觉得这很奇怪,因为如果它被注释掉,我应该得到它。