Flask定制命令

Flask定制命令

将函数或方法定制成命令行代码

python 复制代码
# 关键字绑定i
@click.argument('id')
@app.cli.command('create_user')
# 将i作为参数
def create_user(id):
    conn = pymysql.connect(
        user='root',
        password="777",
        host='127.0.0.1',
        database='flaskdemo',
        port=3306,
        autocommit=False)
    cursor = conn.cursor(pymysql.cursors.DictCursor)
    sql = 'select * from users where id >%s'
    # 查询id大于i的数据并打印
    cursor.execute(sql, id)
    res = cursor.fetchall()
    print(res)

终端输入命令

bash 复制代码
flask --app demo1:app create_user 1
  • demo1:当前项目文件
  • create_user:自定制命令名
  • 1:传递的参数,也就是id

控制台打印

bash 复制代码
(venv) PS D:\Users\Desktop\LnhStudy\FlaskPJT\FlaskDemo1> flask --app demo1:app create_user 1

[{'id': 2, 'name': '李四', 'age': 19}, {'id': 3, 'name': '陈五', 'age': 18}]
相关推荐
To_OC2 小时前
手写 AI 编程 Agent 的命令执行工具:我被 child_process 坑出来的实战经验
后端·node.js·agent
向日的葵0064 小时前
langchain的Tools教程(三)
python·langchain·tools
逝水无殇5 小时前
C# 异常处理详解
开发语言·后端·c#
言乐66 小时前
Python实现可运行解密游戏游戏框架
python·游戏·小程序·游戏程序·关卡设计
YUS云生6 小时前
Python学习笔记·第31天:FastAPI入门——路由、路径参数、查询参数与请求体
笔记·python·学习
智写-AI6 小时前
真实有效的免费降英文AI工具服务商
人工智能·python
yuhuofei20217 小时前
【Python入门】了解掌握Python中函数的基本使用
python
考虑考虑7 小时前
Sentinel安装
java·后端·微服务
IT_陈寒7 小时前
SpringBoot自动配置失灵?你可能忘了这个关键注解
前端·人工智能·后端
碎碎念_4927 小时前
SpringBoot + Vue 前后端分离从 0 到 1 完整环境配置流程
vue.js·spring boot·后端