-
Notifications
You must be signed in to change notification settings - Fork 0
/
database_utils.py
142 lines (118 loc) · 3.97 KB
/
database_utils.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
from pathlib import Path
import sqlite3
def makeDatabase(path):
path = Path(path)
Path.mkdir(path.parent, exist_ok=True, parents=True)
if path.exists():
raise ValueError(f"That database ({path}) already exists and would be overwritten. Exiting")
conn = sqlite3.connect(str(path))
cursor = conn.cursor()
return conn, cursor
def initializeDatabase(conn, cursor):
cursor.execute("""
CREATE TABLE projects (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title text,
subtitle text,
content text,
url text,
likes integer
);
""")
cursor.execute("""
CREATE TABLE participants (
username text primary key,
name text,
skills text,
interests text,
image text
);
""")
cursor.execute("""
CREATE TABLE images (
project integer,
imageUrl text,
FOREIGN KEY (project) REFERENCES
projects (id)
);
""")
cursor.execute("""
CREATE TABLE updates (
project integer,
author text,
content text,
date date,
FOREIGN KEY (project) REFERENCES
projects (id),
FOREIGN KEY (author) REFERENCES
participants (username)
);
""")
cursor.execute("""
CREATE TABLE participation (
project integer,
participant text,
FOREIGN KEY (project) REFERENCES
projects (id),
FOREIGN KEY (participant) REFERENCES
participants (username)
);
""")
cursor.execute("""
CREATE TABLE following (
user text,
follows text,
FOREIGN KEY (user) REFERENCES
participants (username),
FOREIGN KEY (follows) REFERENCES
participants (username)
);
""")
cursor.execute("""
CREATE TABLE technologies (
name text primary key
);
""")
cursor.execute("""
CREATE TABLE project_uses (
project integer,
tech integer,
FOREIGN KEY (project) REFERENCES
projects (id),
FOREIGN KEY (tech) REFERENCES
technologies (id)
);
""")
conn.commit()
def insertProject(cursor, title, subtitle, content, url, likes):
cursor.execute("INSERT INTO projects VALUES (?,?,?,?,?,?)", (None, title, subtitle, content, url, likes))
return cursor.lastrowid
def insertParticipant(cursor, username, name, skills, interests, image):
try:
cursor.execute("INSERT INTO participants VALUES (?,?,?,?,?)", (username, name, skills, interests, image))
except sqlite3.IntegrityError as e:
print(f"Could not add {username} already existing.")
finally:
return username
def insertImage(cursor, project_id, url):
cursor.execute("INSERT INTO images VALUES (?,?)", (project_id, url))
def insertUpdate(cursor, project_id, author, content, date):
cursor.execute("INSERT INTO updates VALUES (?,?,?,?)", (project_id, author, content, date))
def insertParticipation(cursor, project_id, participant):
cursor.execute("INSERT INTO participation VALUES (?,?)", (project_id, participant))
def insertFollowing(cursor, user, follows):
cursor.execute("INSERT INTO following VALUES (?,?)", (user, follows))
def insertTechnology(cursor, name):
try:
cursor.execute("INSERT INTO technologies VALUES (?)", (name))
except sqlite3.IntegrityError:
print(f"Could not add Tech {name} already existing.")
finally:
return name
def insertProjectUses(cursor, project_id, tech_name):
cursor.execute("INSERT INTO project_uses VALUES (?,?)", (project_id, tech_name))
def test_db_instanciation():
conn, cursor = makeDatabase("test_db.db")
initializeDatabase(conn, cursor)
if __name__ == "__main__":
test_db_instanciation()