-
Notifications
You must be signed in to change notification settings - Fork 0
/
database_manager.py
146 lines (130 loc) · 6.74 KB
/
database_manager.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import sqlite3
from sqlite3 import Error
def create_connection():
try:
connection = sqlite3.connect('shooting_club', detect_types=sqlite3.PARSE_DECLTYPES)
connection.execute('PRAGMA foreign_keys = 1')
print('Connected to database. SQLite: ' + sqlite3.version)
return connection
except Error as e:
print(e)
def close_connection(connection):
try:
connection.close()
print('Connection closed. SQLite: ' + sqlite3.version)
except Error as e:
print(e)
def execute_sql(sql, *args):
print(sql)
print(args)
connection = create_connection()
try:
cursor = connection.cursor()
cursor.execute(sql, *args)
if sql.lstrip().upper().startswith('SELECT'):
rows = cursor.fetchall()
print('Successfully executed SQL')
close_connection(connection)
return rows
else:
connection.commit()
print('Successfully executed and committed SQL')
close_connection(connection)
return True
except Error as e:
print(e)
connection.rollback()
print('Rolled back entry')
print('Rolled back entry')
close_connection(connection)
return False
statement1 = '''CREATE TABLE IF NOT EXISTS user (
type INTEGER NOT NULL,
first_name TEXT NOT NULL,
last_name TEXT NOT NULL,
date_of_birth INTEGER NOT NULL,
address TEXT NOT NULL,
city TEXT NOT NULL,
post_code TEXT NOT NULL,
telephone_number TEXT NOT NULL,
email_address TEXT UNIQUE,
password TEXT,
knsa_licence_number INTEGER PRIMARY KEY,
date_of_membership INTEGER NOT NULL);'''
statement2 = '''CREATE TABLE IF NOT EXISTS firearm (
type TEXT PRIMARY KEY);'''
statement8 = '''CREATE TABLE IF NOT EXISTS discipline (
type TEXT PRIMARY KEY);'''
statement9 = '''CREATE TABLE IF NOT EXISTS rank (
user INTEGER NOT NULL,
discipline TEXT NOT NULL,
class TEXT NOT NULL,
FOREIGN KEY (user) REFERENCES user (knsa_licence_number)
ON DELETE NO ACTION ON UPDATE CASCADE,
FOREIGN KEY (discipline) REFERENCES discipline (type)
ON DELETE NO ACTION ON UPDATE CASCADE,
PRIMARY KEY (user, discipline, class));'''
statement3 = '''CREATE TABLE IF NOT EXISTS ammunition (
type TEXT PRIMARY KEY,
price REAL NOT NULL,
stock INTEGER NOT NULL);'''
statement4 = '''CREATE TABLE IF NOT EXISTS scorecard (
type TEXT PRIMARY KEY,
price REAL NOT NULL,
stock INTEGER NOT NULL);'''
statement5 = '''CREATE TABLE IF NOT EXISTS score (
id INTEGER PRIMARY KEY AUTOINCREMENT,
card_one_shot_one INTEGER NOT NULL,
card_one_shot_two INTEGER NOT NULL,
card_one_shot_three INTEGER NOT NULL,
card_one_shot_four INTEGER NOT NULL,
card_one_shot_five INTEGER NOT NULL,
card_one_total INTEGER NOT NULL,
card_two_shot_one INTEGER NOT NULL,
card_two_shot_two INTEGER NOT NULL,
card_two_shot_three INTEGER NOT NULL,
card_two_shot_four INTEGER NOT NULL,
card_two_shot_five INTEGER NOT NULL,
card_two_total INTEGER NOT NULL,
date INTEGER NOT NULL,
shooter INTEGER NOT NULL,
submitter INTEGER NOT NULL,
discipline TEXT NOT NULL,
firearm TEXT NOT NULL,
own_firearm INTEGER NOT NULL,
FOREIGN KEY (shooter) REFERENCES user (knsa_licence_number)
ON DELETE NO ACTION ON UPDATE CASCADE,
FOREIGN KEY (firearm) REFERENCES firearm (type)
ON DELETE NO ACTION ON UPDATE CASCADE,
FOREIGN KEY (discipline) REFERENCES discipline (type)
ON DELETE NO ACTION ON UPDATE CASCADE,
FOREIGN KEY (submitter) REFERENCES user (knsa_licence_number)
ON DELETE NO ACTION ON UPDATE CASCADE);'''
statement6 = '''CREATE TABLE IF NOT EXISTS sale_scorecard (
id INTEGER PRIMARY KEY AUTOINCREMENT,
date INTEGER NOT NULL,
quantity INTEGER NOT NULL,
type TEXT NOT NULL,
seller INTEGER NOT NULL,
buyer INTEGER NOT NULL,
price REAL NOT NULL,
FOREIGN KEY (type) REFERENCES scorecard (type)
ON DELETE NO ACTION ON UPDATE CASCADE,
FOREIGN KEY (seller) REFERENCES user (knsa_licence_number)
ON DELETE NO ACTION ON UPDATE CASCADE,
FOREIGN KEY (buyer) REFERENCES user (knsa_licence_number)
ON DELETE NO ACTION ON UPDATE CASCADE);'''
statement7 = '''CREATE TABLE IF NOT EXISTS sale_ammunition (
id INTEGER PRIMARY KEY AUTOINCREMENT,
date INTEGER NOT NULL,
quantity INTEGER NOT NULL,
type TEXT NOT NULL,
seller INTEGER NOT NULL,
buyer INTEGER NOT NULL,
price REAL NOT NULL,
FOREIGN KEY (type) REFERENCES ammunition (type)
ON DELETE NO ACTION ON UPDATE CASCADE,
FOREIGN KEY (seller) REFERENCES user (knsa_licence_number)
ON DELETE NO ACTION ON UPDATE CASCADE,
FOREIGN KEY (buyer) REFERENCES user (knsa_licence_number)
ON DELETE NO ACTION ON UPDATE CASCADE);'''