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

来源:5-3 网易新闻框架搭建

沧海遗珠丶丶

2019-09-09

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回答

NavCat

2019-09-09

使用命令行安装:

pip install mysqlclient

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

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


0
4
NavCat
回复
沧海遗珠丶丶
分享个经验:调用MySQL数据如果出现错误,返回的错误中会有一个对应的编码的,像这里就是1045,每个编码对应有不同的含义,去百度搜索“Mysql 1045” 这种类型的关键词就能找到解决方案
2019-09-10
共4条回复

Python操作三大主流数据库-MySQL+MongoDB+Redis

一次实战同时掌握Python操作MySQL,MongoDB,Redis 三大数据库使用技巧

2024 学习 · 376 问题

查看课程