#๐Ÿ”’ Error with Flask DB Initialization

15 messages ยท Page 1 of 1 (latest)

viscid jasper
#

I am going through the flask documentation and i have encountered an error, I have put it on stackoverflow and the response didnt help either, so this is my only option left. If anyone has had this error or can put me through the solution would be highly appreciated!

I am trying to initialize the sqlite database. This is my db.py file


import click
from flask import current_app, g

def get_db():
    if "db" not in g:
        g.db = sqlite3.connect(
            current_app.config["DATABASE"],
            detect_types= sqlite3.PARSE_DECLTYPES 
        )
        g.db.row_factory = sqlite3.Row
        
    return g.db

def close_db(e = None):
    db = g.pop("db", None)
    
    if db is not None:
        db.close()
        
def init_db():
    db = get_db()
    
    with current_app.open_resources("schema.sql") as f:
        db.executescript(f.read().decode("utf8"))
        
@click.command('init-db')
def init_db_command():
    """Clear the existing data and create new tables"""
    init_db()
    click.echo("Initialized the database")
        ```
polar rapidsBOT
#

Hey @viscid jasper!

It looks like you pasted Python code without syntax highlighting.

Please use syntax highlighting to improve the legibility of your code and make it easier for us to help you.

To do this, use the following method:
```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
You can **edit your original message** to correct your code block.
polar rapidsBOT
#

@viscid jasper

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

viscid jasper
#
from flask import Flask
from . import db
from flask_migrate import Migrate


app = Flask(__name__)
migrate = Migrate(app, db)

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

    #from . import db
    db.init_app(app)
    
    return app

# a simple page that says hello
@app.route('/hello')
def hello():
    return 'Hello, World! I am here again'
marsh lichen
#

What was the error and can you link to stack overflow

viscid jasper
#

error Usage: flask [OPTIONS] COMMAND [ARGS]...
Try 'flask --help' for help.

Error: No such command 'init_db'.

#

the highest scored answer is installing a module which the person said was mentioned when he /she ran 'flask --app', when i run it, i dont get a missing module

#

Usage: flask [OPTIONS] COMMAND [ARGS]...

A general utility script for Flask applications.

An application to load must be given with the '--app' option, 'FLASK_APP'
environment variable, or with a 'wsgi.py' or 'app.py' file in the current
directory.

Options:
-e, --env-file FILE Load environment variables from this file. python-
dotenv must be installed.
-A, --app IMPORT The Flask application or factory function to load, in
the form 'module:name'. Module can be a dotted import
or file path. Name is not required if it is 'app',
'application', 'create_app', or 'make_app', and can be
'name(args)' to pass arguments.
--debug / --no-debug Set debug mode.
--version Show the Flask version.
--help Show this message and exit.

Commands:
db Perform database migrations.
routes Show the routes for the app.
run Run a development server.
shell Run a shell in the app context.

marsh lichen
#

What happens if you pass --app?

viscid jasper
#

no such command init-db

viscid jasper
polar rapidsBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.