在Flask中使用sqlite的示例

本文介绍了如何在Flask应用中结合Flask_script扩展来管理sqlite数据库。通过manage.py文件,可以实现启动开发服务器、Python shell交互、创建数据库、保存数据以及查询所有记录等命令行任务。
摘要由CSDN通过智能技术生成

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 i
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值