database.py
from flask_sqlalchemy import SQLAlchemy db = SQLAlchemy()
config.py
import os from database import db from flask import Flask basedir=os.path.abspath(os.path.dirname(__name__)) app = Flask(__name__) #Dabase configuration app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('DATABASE_URI') or 'sqlite:///'+ os.path.join(basedir,'app.db') app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False db.init_app(app)
models.py
from database import db from config import app class StudentMore(db.Model): __tablename__= 'Students' id = db.Column(db.Integer, primary_key=True, autoincrement=True) name = db.Column(db.String(108), nullable=False) score = db.Column(db.Integer, nullable=False) def __repr__(self): return 'StudentMore %r' % self.name with app.app_context(): db.create_all()
标签:__,sqlalchemy,建表,flask,app,db,import,os From: https://www.cnblogs.com/luoye00/p/18453997