Skip to content

Commit

Permalink
set up db
Browse files Browse the repository at this point in the history
  • Loading branch information
SaraBee committed May 27, 2020
1 parent 693f743 commit 15bbddb
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 9 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# MCGJ

This is

Data Model Notes
2 changes: 2 additions & 0 deletions __init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from flask import Flask
app = Flask(__name__)
82 changes: 82 additions & 0 deletions db.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import sqlite3
import json
import click
from flask import Flask, current_app, g
from flask.cli import with_appcontext

DATABASE = "mcg.db"
app = Flask(__name__)

def get_db():
db = getattr(g, "_database", None)
if db is None:
db = g._database = sqlite3.connect(DATABASE)
def dict_factory(cursor, row):
d = {}
for idx, col in enumerate(cursor.description):
d[col[0]] = row[idx]
return d
db.row_factory = dict_factory
return db


@app.teardown_appcontext
def close_connection(exception):
db = getattr(g, "_database", None)
if db is not None:
db.close()


def init_db():
with app.app_context():
db = get_db()
with app.open_resource("init-db.sql") as f:
db.cursor().executescript(f.read())
db.commit()

@click.command("init-db")
@with_appcontext
def init_db_command():
"""DESTROY existing data and create a new table."""
init_db()
click.echo("Initialized the database.")


def print_records(table: str) -> str:
db = get_db()
cursor = db.cursor()

command = """
SELECT * FROM "{}";
""".format(table)

results = []
for row in cursor.execute(command):
results.append(row)

return(json.dumps(results))




if __name__ == "__main__":
db = sqlite3.connect(DATABASE)

def dict_factory(cursor, row):
d = {}
for idx, col in enumerate(cursor.description):
d[col[0]] = row[idx]
return d
db.row_factory = dict_factory

cursor = db.cursor()

command = """
SELECT * FROM tracks
"""

tracks = []
for track in cursor.execute(command):
tracks.append(track)

print(tracks)
10 changes: 10 additions & 0 deletions demo.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
-- Demo row table
INSERT INTO sessions (id, create_date, update_date, name, date, spotify_url, current_round)
VALUES (1, 1590518158 , 1590518159, "MCG 2020-05-26", 1590518158, "https:
//open.spotify.com/playlist/5BTObgLJeCfLaP8unbefIs?si=q2zzZR4RTbOpkJchCMq2Cw", 1);

INSERT INTO tracks (id, create_date, update_date, person, track_name, track_url, session_id, done, round_number, round_position)
VALUES (1, 1590518159, 1590518160, "Sara Bobo", "MissDat†Booty†", "https://open.spotify.com/track/4UJIkpP55qZuq1ecP5luqQ?si=E44FBYM0SXmwAuCM0dZ_wg", 1, 1, 1, 0);

INSERT INTO tracks (id, create_date, update_date, person, session_id, done, round_number)
VALUES (2, 1590518159, 1590518160, "Toph Allen", 1, 0, 1);
9 changes: 0 additions & 9 deletions helloworld.py

This file was deleted.

8 changes: 8 additions & 0 deletions index.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from flask import Flask
from .db import print_records

app = Flask(__name__)

@app.route('/')
def print_tracks():
return print_records('tracks')
22 changes: 22 additions & 0 deletions init-db.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
CREATE TABLE sessions (
id INTEGER PRIMARY KEY,
create_date INTEGER NOT NULL,
update_date INTEGER,
name TEXT NOT NULL,
date INTEGER,
spotify_url TEXT,
current_round INTEGER
);

CREATE TABLE tracks (
id INTEGER PRIMARY KEY,
create_date INTEGER NOT NULL,
update_date INTEGER,
person TEXT,
track_name TEXT,
track_url TEXT,
session_id INTEGER,
done INTEGER,
round_number INTEGER,
round_position INTEGER
);

0 comments on commit 15bbddb

Please sign in to comment.