时间:2021-07-01 10:21:17 帮助过:22人阅读
# coding:utf-8 from flask import Flask from flask_sqlalchemy import SQLAlchemy app = Flask(__name__,template_folder=‘templates‘) # [‘SQLALCHEMY_DATABASE_URL‘] = ‘mysql+mysqlconnector://root:123456@localhost:3306/student‘‘‘ app.config.from_object(‘config‘) db = SQLAlchemy(app) class user_database(db.Model): id = db.Column(db.INT,primary_key=True) name = db.Column(db.String(255),unique=False) sex = db.Column(db.String(10),unique=False) def __init__(self,id,name,sex): self.id = id self.name = name self.sex = sex # 创建数据库 db.create_all() db.session.commit() # 实例化插入数据库 text_1 = user_database(1,‘八戒‘,‘男‘) # db.session.add(user_database(2,‘猴哥‘,‘man‘)) # db.session.add(user_database(3,‘物镜‘,‘man‘)) # db.session.add(user_database(4,‘师父‘,‘man‘)) db.session.add(user_database(5,‘李大郎‘,‘man‘)) db.session.commit()
flask_sqlalchemy的使用
标签:pass driver utf-8 def class root creat sql roo