Skip to content
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 82 lines (67 sloc) 2.07 KB
import logging
import sqlite3
import time
from flask import Flask, request, g
logging.basicConfig(filename='bartdb.log', level=logging.INFO)
DATABASE = 'bart.db'
app = Flask(__name__)
def connect_to_database():
return sqlite3.connect(app.config['DATABASE'])
def get_db():
db = getattr(g, 'db', None)
if db is None:
db = g.db = connect_to_database()
return db
def close_connection(exception):
db = getattr(g, 'db', None)
if db is not None:
def execute_query(query, args=()):
cur = get_db().execute(query, args)
rows = cur.fetchall()
return rows
def viewdb():
return '<br>'.join(str(row) for row in execute_query(
FROM etd
LIMIT 1000"""
def view_schema():
return '<br>'.join(str(row) for row in execute_query(
"""pragma table_info('etd')"""
def print_data():
"""Respond to a query of the format:
with ETD data for the time and location specified in the query"""
start_time = time.time()
cur = get_db().cursor()
minute_of_day = int(request.args.get('time'))
except ValueError:
return "Time must be an integer"
station = request.args.get('station')
day = request.args.get('day')
dest = request.args.get('dest')
result = execute_query(
"""SELECT etd, count(*)
FROM etd
WHERE dest = ? AND minute_of_day = ?
AND station = ? AND day_of_week = ?
GROUP BY etd""",
(dest, minute_of_day, station, day)
str_rows = [','.join(map(str, row)) for row in result]
query_time = time.time() - start_time"executed query in %s" % query_time)
header = 'etd,count\n'
return header + '\n'.join(str_rows)
if __name__ == "__main__":
You can’t perform that action at this time.