wnhuiyou 发表于 2021-1-28 23:36:59

Flask连接Mysql数据库一直报错

代码如下:
from flask import Flask
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)

app.config['SQLALCHEMY_DATABASE_URI'] = "mysql://root:rootroot@localhost:3306/flask"

app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True

db = SQLAlchemy(app)


class Role(db.Model):
    """用户角色/身份表"""
    __tablename__ = "tbl_roles"
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(32), unique=True)    # unique 表示不允许出现重复的值
    users = db.relationship("User", backref="role")


class User(db.Model):
    __tablename__ = "tbl_users"
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(63), unique=True)
    email = db.Column(db.String(128), unique=True)
    password = db.Column(db.String(128))
    role_id = db.Column(db.Integer, db.ForeignKey("tbl_roles.id"))


if __name__ == '__main__':
    db.drop_all()
    db.create_all()



数据库的地址和账户密码都是对的,在创建表的时候一直报错,找不到原因,报错如下:

(venv) D:\flaskProject1>python mysql.py
Traceback (most recent call last):
File "mysql.py", line 31, in <module>
    db.drop_all()
File "D:\flaskProject1\venv\lib\site-packages\flask_sqlalchemy\__init__.py", line 1047, in drop_all
    self._execute_for_all_tables(app, bind, 'drop_all')
File "D:\flaskProject1\venv\lib\site-packages\flask_sqlalchemy\__init__.py", line 1031, in _execute_for_all_tables
    op(bind=self.get_engine(app, bind), **extra)
File "D:\flaskProject1\venv\lib\site-packages\flask_sqlalchemy\__init__.py", line 962, in get_engine
    return connector.get_engine()
File "D:\flaskProject1\venv\lib\site-packages\flask_sqlalchemy\__init__.py", line 555, in get_engine
    options = self.get_options(sa_url, echo)
File "D:\flaskProject1\venv\lib\site-packages\flask_sqlalchemy\__init__.py", line 570, in get_options
    self._sa.apply_driver_hacks(self._app, sa_url, options)
File "D:\flaskProject1\venv\lib\site-packages\flask_sqlalchemy\__init__.py", line 884, in apply_driver_hacks
    sa_url.query.setdefault('charset', 'utf8')
AttributeError: 'sqlalchemy.cimmutabledict.immutabledict' object has no attribute 'setdefault'


实在是找不到解决的办法了,请教下各位。

wp231957 发表于 2021-1-29 08:27:07

这个库没用过from flask_sqlalchemy import SQLAlchemy不知道是干啥的

几点:
1、mysql是否安装正确各种相关服务是否已启动
2、我用python连mysql 也没像你这样麻烦啊
3、一般表不都是事先建立好了 在用,很少有通过代码建表的吧’

wnhuiyou 发表于 2021-1-29 09:29:07

wp231957 发表于 2021-1-29 08:27
这个库没用过from flask_sqlalchemy import SQLAlchemy不知道是干啥的

几点:


单独用Python连接mysql没有问题,但就只用flask框架连接mysql的时候报错,哪个库其实我也不知道干嘛,感觉好像是用来连接数据库的,我是看网上的教程写的...

wnhuiyou 发表于 2021-1-30 15:58:31

没人用过 flask 连接MySQL的吗?我真的是直接从CSDN上抄作业然后运行都报错,真是找不到什么其他原因了,谁能帮我远程看看呀,这步不弄好,下面无法接着学习flask框架了

wnhuiyou 发表于 2021-1-30 16:16:40

Traceback (most recent call last):
File "flask_mysql.py", line 37, in <module>
    db.drop_all()
File "D:\flaskProject1\venv\lib\site-packages\flask_sqlalchemy\__init__.py", line 1047, in drop_all
    self._execute_for_all_tables(app, bind, 'drop_all')
File "D:\flaskProject1\venv\lib\site-packages\flask_sqlalchemy\__init__.py", line 1031, in _execute_for_all_tables
    op(bind=self.get_engine(app, bind), **extra)
File "D:\flaskProject1\venv\lib\site-packages\flask_sqlalchemy\__init__.py", line 962, in get_engine
    return connector.get_engine()
File "D:\flaskProject1\venv\lib\site-packages\flask_sqlalchemy\__init__.py", line 555, in get_engine
    options = self.get_options(sa_url, echo)
File "D:\flaskProject1\venv\lib\site-packages\flask_sqlalchemy\__init__.py", line 570, in get_options
    self._sa.apply_driver_hacks(self._app, sa_url, options)
File "D:\flaskProject1\venv\lib\site-packages\flask_sqlalchemy\__init__.py", line 884, in apply_driver_hacks
    sa_url.query.setdefault('charset', 'utf8')
AttributeError: 'sqlalchemy.cimmutabledict.immutabledict' object has no attribute 'setdefault'


这到底是什么错误呀,我一直没看明白

°蓝鲤歌蓝 发表于 2021-2-2 23:59:43

本帖最后由 °蓝鲤歌蓝 于 2021-2-3 00:00 编辑

这个库的版本问题,换个旧一点的版本。

wnhuiyou 发表于 2021-2-4 09:21:13

°蓝鲤歌蓝 发表于 2021-2-2 23:59
这个库的版本问题,换个旧一点的版本。

谢谢你,就是你说的版本问题,我降低SQLAlchemy 版本后,就好了... 真坑!
页: [1]
查看完整版本: Flask连接Mysql数据库一直报错