初学Python定义数据库出现的问题

初学者在创建Python Flask项目时遇到数据库初始化问题。通过创建文件夹、__init__.py、db.py和schema.sql文件,然后在终端设置FLASK_APP和FLASK_ENV环境变量并尝试运行`flask init-db`,但出现错误提示"No such command ‘init-db’."。问题在于命令执行的目录不正确,需要回到flaskr项目上一级目录执行`flask init-db`,之后错误得以解决。


注:前提使用 Flask 框架

首先创建文件夹

文件夹名为 flaskr

创建__init__.py文件

文件内容:

import os

from flask import Flask


def create_app(test_config=None):
    # create and configure the app
    app = Flask(__name__, instance_relative_config=True)
    app.config.from_mapping(
        SECRET_KEY='dev',
        DATABASE=os.path.join(app.instance_path, 'flaskr.sqlite'),
    )

    if test_config is None:
        # load the instance config, if it exists, when not testing
        app.config.from_pyfile('config.py', silent=True)
    else:
        # load the test config if passed in
        app.config.from_mapping(test_config)

    # ensure the instance folder exists
    try:
        os.makedirs(app.instance_path)
    except OSError:
        pass

    # a simple page that says hello
    @app.route('/hello')
    def hello():
        return 'Hello, World!'

    from . import db
    db.init_a
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值