Browse Source

Upload SQL

Tatiana Castro 6 years ago
parent
commit
0e7447c037
3 changed files with 75 additions and 0 deletions
  1. 8
    0
      __init__.py
  2. 41
    0
      db.py
  3. 26
    0
      schema.sql

+ 8
- 0
__init__.py View File

@@ -0,0 +1,8 @@
1
+def create_app():
2
+    app = ...
3
+    # existing code omitted
4
+
5
+    from . import db
6
+    db.init_app(app)
7
+
8
+    return app

+ 41
- 0
db.py View File

@@ -0,0 +1,41 @@
1
+import sqlite3
2
+
3
+import click
4
+from flask import current_app, g
5
+from flask.cli import with_appcontext
6
+
7
+
8
+def get_db():
9
+    if 'db' not in g:
10
+        g.db = sqlite3.connect(
11
+            current_app.config['DATABASE'],
12
+            detect_types=sqlite3.PARSE_DECLTYPES
13
+        )
14
+        g.db.row_factory = sqlite3.Row
15
+
16
+    return g.db
17
+
18
+
19
+def close_db(e=None):
20
+    db = g.pop('db', None)
21
+
22
+    if db is not None:
23
+        db.close()
24
+def init_db():
25
+    db = get_db()
26
+
27
+    with current_app.open_resource('schema.sql') as f:
28
+        db.executescript(f.read().decode('utf8'))
29
+
30
+
31
+@click.command('init-db')
32
+@with_appcontext
33
+def init_db_command():
34
+    """Clear the existing data and create new tables."""
35
+    init_db()
36
+    click.echo('Initialized the database.')
37
+
38
+def init_app(app):
39
+    app.teardown_appcontext(close_db)
40
+    app.cli.add_command(init_db_command)
41
+

+ 26
- 0
schema.sql View File

@@ -0,0 +1,26 @@
1
+
2
+CREATE TABLE station (
3
+  s_id INT PRIMARY KEY AUTOINCREMENT,
4
+  nombre_empleado CHAR(20)  NOT NULL,
5
+  oficina CHAR(20) NOT NULL,
6
+  last_turn INT NOT NULL
7
+);
8
+
9
+CREATE TABLE user (
10
+  u_id INT PRIMARY KEY AUTOINCREMENT,
11
+  uName CHAR(20) NOT NULL,
12
+  isAdmin BIT NOT NULL,
13
+  email CHAR(30) NOT NULL
14
+);
15
+
16
+CREATE TABLE turno(
17
+ t_id INT PRIMARY KEY AUTOINCREMENT,
18
+ cName CHAR(20) NOT NULL, 
19
+ cEmail CHAR(30) NOT NULL, 
20
+ purpose CHAR(40), 
21
+ timeArrival CHAR(5) NOT NULL, 
22
+ timeEnter CHAR(5),
23
+ station INT NOT NULL, 
24
+ FOREIGN KEY (station) REFERENCES station (id) 
25
+);
26
+