【问题标题】:Cannot connect Flask application with MySQL database [duplicate]无法将 Flask 应用程序与 MySQL 数据库连接 [重复]
【发布时间】:2020-03-15 13:58:15
【问题描述】:

我运行了这个应用程序,但它没有连接到 MySQL 数据库。我正在使用 Python 3.7.4 版和 Flask-SQLAlchemy 2.4.1 版

from flask import Flask , render_template, request

from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)       
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:@localhost/codingthunder'
db = SQLAlchemy(app)


class Contacts(db.Model):               **# Table in database = contacts**

    sno = db.Column(db.Integer , primary_key=True)   **# sno = Primary key** 

    name = db.Column(db.String(80),unique=False, nullable=False)

    def __repr__(self):
        return f"<User> {self.name}"

@app.route("/")             #defining route
def home():
    return render_template("/index.html")

@app.route("/contact" , methods = ['GET','POST'])     

def contact():
    if request.method == 'POST':
        entry = Contacts(name = "ashfaq")
        db.session.add(entry)
        db.session.commit()
    return render_template("contact.html")        

app.run(debug = True)           

错误是 ModuleNotFoundError: 没有名为“MySQLdb”的模块

【问题讨论】:

  • 您自己的粗体错误不是回答您自己的问题吗?显然你必须熟悉安装 python 模块

标签: python mysql flask sqlalchemy


【解决方案1】:

你可以试试这个:pip install pymysql

此命令将尝试安装包:pymysql,之前您的程序中缺少该包。

【讨论】:

  • 我已经安装了 PyMySQL 库但还是同样的问题
  • 正确答案是:pip install mysqldbmodel
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-11-26
  • 1970-01-01
  • 1970-01-01
  • 2019-07-23
  • 2012-10-09
  • 2020-04-23
  • 1970-01-01
相关资源
最近更新 更多