Flask_script是一个在flask应用外部编写脚本的扩展
常用功能:1、运行一个开发的服务器。2、Python shell 中操作数据库。3、命令行任务
manage.py:
from flask_script import Manager
from app import app
import sqlite3
from models import User
manager = Manager(app)
# python manege.py hello
@manager.command
def hello():
print('hello world')
@manager.option('-m', '--msg', dest='msg_val', default='world')
def hello_world(msg_val):
print('hello' + msg_val)
@manager.command
def init_db():
sql = 'create table users(id INT,name TEXT)'
conn = sqlite3.connect('test.db')
cursor = conn.cursor()
cursor.execute(sql)
conn.commit()
cursor.close()
conn.close()
@manager.command
def save():
user = User(1, 'useless')
user.save()
@manager.command
def query_all():
users = User.query()
for user in users:
print(user)
if __name__ == "__main__":
manager.run()
models.py

本文介绍了如何在Flask应用中结合Flask_script扩展来管理sqlite数据库。通过manage.py文件,可以实现启动开发服务器、Python shell交互、创建数据库、保存数据以及查询所有记录等命令行任务。
最低0.47元/天 解锁文章

3577

被折叠的 条评论
为什么被折叠?



