当前位置:网站首页>Flask failed to create database without error

Flask failed to create database without error

2022-08-09 06:28:00 Koala Rice Bowl

The code that the teacher gave us to create a database:

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
app=Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI']='mysql://root:[email protected]:3306/text'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS']=False
app.config['SQLALCHEMY_ECHO'] = True
db=SQLAlchemy(app)

class Role(db.Model):
    __tablename__='roles'
    id=db.Column(db.Integer,primary_key=True)
    name=db.Column(db.String(16),unique=True)


class User(db.Model):
    __tablename__='users'
    id=db.Column(db.Integer,primary_key=True)
    name=db.Column(db.String(16),unique=True)
    role_id=db.Column(db.Integer,db.ForeignKey('roles.id'))


@app.route('/',methods=['post','get'])
def index():
    return 'hello'


# 删除表
db.drop_all()
# 创建表
db.create_all()

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

This above code maypython2.xRunning is correct but correct with what I installedpython环境是3.8It will run unsuccessfully but it will not report an error in the console
这是因为MySQLdb 只适用于python2.x,发现pip装不上.它在python3的替代品是: import pymysql
Add the following code to the import package

import pymysql
pymysql.install_as_MySQLdb()

看图:

在这里插入图片描述

原网站

版权声明
本文为[Koala Rice Bowl]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/221/202208090622259098.html

随机推荐