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}]
相关推荐
TE-茶叶蛋4 分钟前
TF-IDF 与 BM25 深度解析:从理论到项目实战
python·django·tf-idf
xcbrand7 分钟前
湖南VI设计公司排名
大数据·人工智能·python
IT_陈寒15 分钟前
Vite热更新失效?可能你在用Windows
前端·人工智能·后端
lllsure35 分钟前
【开源项目】Learn Claude Code
python·ai
椰椰椰耶1 小时前
[SpringCloud][14]OpenFeign参数传递方法
后端·spring·spring cloud
onething3652 小时前
Spring Boot + Spring AI 从入门到实战:7天转型计划 Day 3 —— 消息表设计 + 级联删除 + 事务管理
人工智能·后端
荣江2 小时前
Hermes Agent 代码仓库打包工具使用指南(repomix-rs 高性能版)
后端
王某某人2 小时前
LangChain4j 入门:Java 程序员的第一个 AI 对话程序
人工智能·后端
码农刚子2 小时前
从零开始:在 Windows 服务器上部署 Node.js 项目(小白实战教程)
后端·node.js
Cache技术分享2 小时前
435. Java 日期时间 API - Clock 灵活获取当前时间
前端·后端