Flask-SQLALchemy插件
另外一个框架,叫Flask-SQLAlchemy,Flask-SQLAlchemy是对SQLAlchemy进行了一个简单的封装,使得我们在flask中使用sqlalchemy更加的简单.
可以通过 :
pip install flask-sqlalchemy 安装
# @ Time : 2020/4/28 23:09
# @ Author : Ellen
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
HOSTNAME = '127.0.0.1'
DATABASE = 'demo0427'
PORT = 3306
USERNAME = 'root'
PASSWORD = 'root'
DB_URL = 'mysql+mysqlconnector://{}:{}@{}:{}/{}?charset?'.format(USERNAME, PASSWORD, HOSTNAME, PORT, DATABASE)
app = Flask(__name__)
# engine = create_engine(DB_URL)
# SQLALCHEMY DATABASE_URI
app.config['SQLALCHEMY_DATABASE_URI'] = DB_URL
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = Flask
db = SQLAlchemy(app)
class User(db.Model):
# 如果不写__tablename__会以模型名的小写来定义表格
__tablename__ = 'users'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
name = db.Column(db.String(50))
# articles = db.relationship('Article')
def __repr__(self):
return "User(name:%s)" % self.name
class Article(db.Model):
__tablename__ = 'article'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
title = db.Column(db.String(50))
uid = db.Column(db.Integer, db.ForeignKey('users.id'))
author = db.relationship('User', backref='articles')
# db.drop_all()
db.create_all()
# 添加数据
# user = User(name='ellen')
# article = Article(title='python')
# article.author = user
# db.session.add(article)
# db.session.commit()
# 查询数据 3种查询方式都可以,第一种最简便
# user = User.query.all()
# print(user)
# users = User.query.order_by(User.id.desc()).all()
# print(users)
# users = db.session.query(User).all()
# print(users)
# 删除数据
user = User.query.filter(User.name == 'ellen').first()
db.session.delete(user)
db.session.commit()
@app.route("/")
def index():
return "首页"
if __name__ == '__main__':
app.run(debug=True)
Flask-Script
Flask-Script的作用是可以通过命令行的形式来操作Flask
例如通过命令跑一个开发版本的服务器、设置数据库,定时任务等。要使用Flask-Script,
可以通过:pip install flask-script 安装最新版本
# @ Time : 2020/4/29
# @ Author : Ellen
from flask_script import Manager
from flask_sqlalchemy_demo import app
manage = Manager(app)
@manage.command
def index():
print("hello")
@manage.option('-n', '--name', dest='name')
@manage.option('-u', '--url', dest='url')
def hello(name, url):
print("hello", name, url)
if __name__ == '__main__':
manage.run()
安装 Flask-Migrate
pip install -i https://pypi.douban.com/simple flask-migrate
创建
# @ Time : 2020/4/29 4:30
# @ Author : Ellen
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
import config
app = Flask(__name__)
app.config.from_object(config)
db = SQLAlchemy(app)
class AdminUser(db.Model):
__tablename__ = 'admin_users'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
name = db.Column(db.String(50))
email = db.Column(db.String(50))
db.create_all()
if __name__ == '__main__':
app.run(debug=True)
创建:
# @ Time : 2020/4/29 4:30
# @ Author : Ellen
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
import config
app = Flask(__name__)
app.config.from_object(config)
db = SQLAlchemy(app)
class AdminUser(db.Model):
__tablename__ = 'admin_users'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
name = db.Column(db.String(50))
email = db.Column(db.String(50))
db.create_all()
if __name__ == '__main__':
app.run(debug=True)
# @ Time : 2020/4/29 4:32
# @ Author : Ellen
HOSTNAME = '127.0.0.1'
DATABASE = 'demo0427'
PORT = 3306
USERNAME = 'root'
PASSWORD = 'root'
DB_URL = 'mysql+mysqlconnector://{}:{}@{}:{}/{}?charset?'.format(USERNAME, PASSWORD, HOSTNAME, PORT, DATABASE)
SQLALCHEMY_DATABASE_URI = DB_URL
SQLALCHEMY_TRACK_MODIFICATIONS = False
# @ Time : 2020/4/29 4:47
# @ Author : Ellen
from flask_script import Manager
from flask_script_demo import app, AdminUser, db
manage = Manager(app)
@manage.option('-u', "--name", dest='name')
@manage.option('-e', "--email", dest='email')
def add_user(name, email):
user = AdminUser(name=name, email=email)
db.session.add(user)
db.session.commit()
if __name__ == '__main__':
manage.run()