Skip to content
Myr

db.py

This file

Path

examples/tutorial/flaskr/db.py

54 lines · 1.2 KiB · Python

Last changed by

Merge pull request #3246 from pallets/reorder-importsDavid Lordbefore adoption3db4697

History of this file

Take it

The walk stopped after 200 commits: lines older than 70b4509 point at it.

Merge pull request #2962 from garenchan/devDavid Lordbefore adoption70b4509

lines 1–3why, one step back
1import sqlite3
23import click

Merge pull request #3246 from pallets/reorder-importsDavid Lordbefore adoption3db4697

lines 4–5why, one step back
4from flask import current_app
5from flask import g

Merge pull request #2962 from garenchan/devDavid Lordbefore adoption70b4509

lines 6–13why, one step back
6from flask.cli import with_appcontext
789def get_db():10 """Connect to the application's configured database. The connection11 is unique for each request and will be reused if this is called12 again.13 """

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 14why, one step back
14 if "db" not in g:

Merge pull request #2962 from garenchan/devDavid Lordbefore adoption70b4509

line 15why, one step back
15 g.db = sqlite3.connect(

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 16why, one step back
16 current_app.config["DATABASE"], detect_types=sqlite3.PARSE_DECLTYPES

Merge pull request #2962 from garenchan/devDavid Lordbefore adoption70b4509

lines 17–26why, one step back
17 )
18 g.db.row_factory = sqlite3.Row1920 return g.db212223def close_db(e=None):24 """If this request connected to the database, close the25 connection.26 """

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 27why, one step back
27 db = g.pop("db", None)

Merge pull request #2962 from garenchan/devDavid Lordbefore adoption70b4509

lines 28–36why, one step back
28
29 if db is not None:30 db.close()313233def init_db():34 """Clear existing data and create new tables."""35 db = get_db()36

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

lines 37–38why, one step back
37 with current_app.open_resource("schema.sql") as f:
38 db.executescript(f.read().decode("utf8"))

Merge pull request #2962 from garenchan/devDavid Lordbefore adoption70b4509

lines 39–40why, one step back
39
40

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 41why, one step back
41@click.command("init-db")

Merge pull request #2962 from garenchan/devDavid Lordbefore adoption70b4509

lines 42–45why, one step back
42@with_appcontext
43def init_db_command():44 """Clear existing data and create new tables."""45 init_db()

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 46why, one step back
46 click.echo("Initialized the database.")

Merge pull request #2962 from garenchan/devDavid Lordbefore adoption70b4509

lines 47–54why, one step back
47
4849def init_app(app):50 """Register database functions with the Flask app. This is called by51 the application factory.52 """53 app.teardown_appcontext(close_db)54 app.cli.add_command(init_db_command)