-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Danny Denenberg
authored and
Danny Denenberg
committed
Oct 28, 2021
0 parents
commit 89ceba2
Showing
6 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/venv | ||
/__pycache__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from flask import Flask, g | ||
import sqlite3 | ||
|
||
app = Flask(__name__) | ||
|
||
def connect_db(): | ||
sql = sqlite3.connect('./database.db') | ||
sql.row_factory = sqlite3.Row | ||
return sql | ||
|
||
|
||
def get_db(): | ||
#Check if DB is there | ||
if not hasattr(g, 'sqlite3'): | ||
g.sqlite3_db = connect_db() | ||
return g.sqlite3_db | ||
|
||
#close the connection to the database automatically | ||
@app.teardown_appcontext | ||
def close_db(error): | ||
#if global object has a sqlite database then close it. If u leave it open noone can access it and gets lost in memory causing leaks. | ||
if hasattr(g, 'sqlite_db'): | ||
g.sqlite3_db.close() | ||
|
||
@app.route('/') | ||
def index(): | ||
return f'<h1>Hello, World!</h1>' | ||
|
||
@app.route('/users') | ||
def viewusers(): | ||
db = get_db() | ||
cursor = db.execute('select id, name, age from people') | ||
results = cursor.fetchall() | ||
return f"<h1>The Id is {results[0]['id']}.<br> The Name is {results[0]['name']}. <br> The age is {results[0]['age']}. </h1>" | ||
|
||
|
||
if __name__ == '__main__': | ||
app.run(debug = True) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import sqlite3 | ||
|
||
import click | ||
from flask import current_app | ||
from flask import g | ||
from flask.cli import with_appcontext | ||
|
||
|
||
def get_db(): | ||
"""Connect to the application's configured database. The connection | ||
is unique for each request and will be reused if this is called | ||
again. | ||
""" | ||
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): | ||
"""If this request connected to the database, close the | ||
connection. | ||
""" | ||
db = g.pop("db", None) | ||
|
||
if db is not None: | ||
db.close() | ||
|
||
|
||
def init_db(): | ||
"""Clear existing data and create new tables.""" | ||
db = get_db() | ||
|
||
with current_app.open_resource("schema.sql") as f: | ||
db.executescript(f.read().decode("utf8")) | ||
|
||
|
||
@click.command("init-db") | ||
@with_appcontext | ||
def init_db_command(): | ||
"""Clear existing data and create new tables.""" | ||
init_db() | ||
click.echo("Initialized the database.") | ||
|
||
|
||
def init_app(app): | ||
"""Register database functions with the Flask app. This is called by | ||
the application factory. | ||
""" | ||
app.teardown_appcontext(close_db) | ||
app.cli.add_command(init_db_command) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
click==8.0.3 | ||
Flask==2.0.2 | ||
itsdangerous==2.0.1 | ||
Jinja2==3.0.2 | ||
MarkupSafe==2.0.1 | ||
Werkzeug==2.0.2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
-- Initialize the database. | ||
-- Drop any existing data and create empty tables. | ||
|
||
DROP TABLE IF EXISTS user; | ||
|
||
CREATE TABLE user ( | ||
id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
name TEXT NOT NULL, | ||
age TEXT NOT NULL | ||
); |