标签:pass driver utf-8 def class root creat sql roo
# coding:utf-8 DIALECT = ‘mysql‘ DRIVER = ‘pymysql‘ USERNAME = ‘root‘ PASSWORD = ‘123456‘ HOST = ‘127.0.0.1‘ PORT = ‘3306‘ DATABASE = ‘student‘ SQLALCHEMY_DATABASE_URI = ‘{}+{}://{}:{}@{}:{}/{}?charset=utf8‘.format( DIALECT, DRIVER, USERNAME, PASSWORD, HOST, PORT, DATABASE ) SQLALCHEMY_COMMIT_ON_TEARDOWN = True SQLALCHEMY_TRACK_MODIFICATIONS = True
# 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()
标签:pass driver utf-8 def class root creat sql roo
原文地址:https://www.cnblogs.com/wuzaipei/p/9860631.html