请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

命令行执行建表,出现:No module named 'MySQLdb' 的报错

from flask import Flask
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:1234@localhost/news?charset=utf-8'
db = SQLAlchemy(app)

class News(db.Model):
    __tablename__ = 'news'
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(200), nullable=False)
    content = db.Column(db.String(2000), nullable=False)
    img_url = db.Column(db.String(200), nullable=False)
    news_type = db.Column(db.Enum('推荐', '百家', '本地', '图片'))
    created_at = db.Column(db.DateTime)
    updated_at = db.Column(db.DateTime)
    is_valid = db.Column(db.Boolean, default=True)

    def __init__(self, title, content, img_url, news_type, created_at, updated_at, is_valid):
        self.title = title
        self.content = content
        self.img_url = img_url
        self.news_type = news_type
        self.created_at = created_at
        self.updated_at = updated_at
        self.is_valid = is_valid

    def __repr__(self):
        return '<title %r>' % self.title

@app.route('/')
def index():
    return 'hello world'

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

命令行执行建表,出现下面:No module named ‘MySQLdb’ 的报错


D:\python\python全栈\flask\手机网页新闻\flask_news>python
Python 3.6.6 (v3.6.6:4cf1f54eb7, Jun 27 2018, 03:37:03) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from flask_news import db
C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\flask_sqlalchemy\__init__.py:835: FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICAT
IONS adds significant overhead and will be disabled by default in the future.  Set it to True or False to suppress this warning.
  'SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and '
>>> from flask_news import db
>>> db.create_all()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\flask_sqlalchemy\__init__.py", line 1033, in create_all
    self._execute_for_all_tables(app, bind, 'create_all')
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\flask_sqlalchemy\__init__.py", line 1025, in _execute_for_all_tables
    op(bind=self.get_engine(app, bind), **extra)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\flask_sqlalchemy\__init__.py", line 956, in get_engine
    return connector.get_engine()
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\flask_sqlalchemy\__init__.py", line 561, in get_engine
    self._engine = rv = self._sa.create_engine(sa_url, options)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\flask_sqlalchemy\__init__.py", line 966, in create_engine
    return sqlalchemy.create_engine(sa_url, **engine_opts)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\sqlalchemy\engine\__init__.py", line 450, in create_engine
    return strategy.create(*args, **kwargs)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\sqlalchemy\engine\strategies.py", line 87, in create
    dbapi = dialect_cls.dbapi(**dbapi_args)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\sqlalchemy\dialects\mysql\mysqldb.py", line 118, in dbapi
    return __import__("MySQLdb")
ModuleNotFoundError: No module named 'MySQLdb'
>>>

正在回答

1回答

使用命令行安装:

pip install mysqlclient

如果出现类似“Microsoft Visual C++ Build Tools”这样的错误,参考下面的链接解决:

https://blog.csdn.net/qq_14998713/article/details/78277052


0 回复 有任何疑惑可以回复我~
  • 提问者 沧海遗珠丶丶 #1
    sqlalchemy.exc.OperationalError
    sqlalchemy.exc.OperationalError: (MySQLdb._exceptions.OperationalError) (1045, "Access denied for user 'root'@'localhost' (using password: YES)")
    
    (Background on this error at: http://sqlalche.me/e/e3q8)
    
    现在浏览器输入 127.0.0.1:5000报这个错
    回复 有任何疑惑可以回复我~ 2019-09-10 09:07:58
  • NavCat 回复 提问者 沧海遗珠丶丶 #2
    1045错误表示的是用户名或者是密码错误
    回复 有任何疑惑可以回复我~ 2019-09-10 09:15:52
  • 提问者 沧海遗珠丶丶 回复 NavCat #3
    是的,找到那个问题了的原因了。现在执行可以建表,也可以db.session.add(obj)也可以插入数据。
    回复 有任何疑惑可以回复我~ 2019-09-10 10:44:56
问题已解决,确定采纳
还有疑问,暂不采纳
意见反馈 帮助中心 APP下载
官方微信