【发布时间】: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