-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqlFunctions.py
207 lines (174 loc) · 4.71 KB
/
sqlFunctions.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# Copyright 2024 by Aude Jegou.
# All rights reserved.
# This file is part of the BooksApp project,
# and is released under the "MIT License Agreement". Please see the LICENSE
# file that should have been included as part of this package.
import sqlite3
from sqlite3 import Error
def create_connection(path):
connection = None
try:
connection = sqlite3.connect(path)
print("Connection to SQLite DB successful")
execute_query(connection, create_users_table)
execute_query(connection, create_themes_table)
execute_query(connection, create_books_table)
execute_query(connection, create_library_table)
print("Created Tables")
except Error as e:
print(f"The error '{e}' occurred")
return connection
def execute_query(connection, query):
cursor = connection.cursor()
try:
cursor.execute(query)
connection.commit()
print("Query executed successfully")
except Error as e:
print(f"The error '{e}' occurred")
def execute_read_query(connection, query):
cursor = connection.cursor()
result = None
try:
cursor.execute(query)
result = cursor.fetchall()
return result
except Error as e:
result = None
return result
def insert_elements_table(c, table, kwargs):
keys = kwargs.keys()
query = "INSERT INTO {} ({}) VALUES (".format(table, ", ".join(keys))
nbr = len(keys)
for i, arg in enumerate(kwargs.values()):
query += "'" + str(arg) + "'"
if i < nbr-1:
query += ", "
query += ");"
execute_query(c, query)
def get_elements(c, elt, tables, joined=None, wher=None, grp=None):
"""
SELECT
description as Post,
COUNT(likes.id) as Likes
FROM
likes,
posts
WHERE
posts.id = likes.post_id
GROUP BY
likes.post_id
"""
if isinstance(elt, dict):
eltlist = ''
t = len(elt)
for i, key in enumerate(elt):
eltlist += key + '.' + elt[key]
if i < t-1:
eltlist += ', '
elif isinstance(elt, list):
eltlist = ', '.join(elt)
elif isinstance(elt, str):
eltlist = elt
init = "SELECT {} FROM {}".format(eltlist, ', '.join(tables))
# tO TEST
if joined is not None:
val = ""
t = len(joined)
for i, key in enumerate(joined):
val += "INNER JOIN " + key + " ON " + joined[key]
if i < t-1:
val += ', '
init += val
if wher is not None:
val = " WHERE "
i = 0
for key in wher:
if i > 0:
val += " AND "
val += key + "='" + str(wher[key]) + "'"
i += 1
init += val
if grp is not None:
val = " GROUP BY {}".format(grp)
init += val
init += ";"
results = execute_read_query(c, init)
return results
def update_table_elt(c, table, elt, wher):
# UPDATE
# employees
# SET
# lastname = 'Smith'
# WHERE
# employeeid = 3;
init = "UPDATE {} SET ".format(table)
if isinstance(elt, dict):
eltlist = ''
t = len(elt)
for i, key in enumerate(elt):
value = elt[key]
if value:
eltlist += key + "='" + value + "'"
if i < t - 1:
eltlist += ', '
if not eltlist:
return
init += eltlist
val = " WHERE "
i = 0
for key, item in wher.items():
if i > 0:
val += ' AND '
if isinstance(item, str):
val += key + "='" + item + "'"
else:
val += key + "=" + str(item)
i += 1
init += val
init += ";"
execute_query(c, init)
def get_header_table(c, table):
query = "SELECT * FROM {}".format(table)
cursor = c.execute(query)
names = [description[0] for description in cursor.description]
return names
# title, creator, description, publisher, subject
create_users_table = """
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT,
password TEXT
);
"""
create_themes_table = """
CREATE TABLE IF NOT EXISTS themes (
subtype TEXT,
type TEXT
);
"""
create_books_table = """
CREATE TABLE IF NOT EXISTS books (
id INTEGER PRIMARY KEY AUTOINCREMENT,
theme TEXT,
author TEXT NOT NULL,
publisher TEXT,
series TEXT,
tome TEXT,
title TEXT NOT NULL,
description TEXT,
language TEXT,
cover TEXT,
FOREIGN KEY (theme) REFERENCES theme (subtype)
);
"""
create_library_table = """
CREATE TABLE IF NOT EXISTS library (
book_id INTEGER,
rating INTEGER,
read TEXT,
user_id INTEGER,
FOREIGN KEY (book_id) REFERENCES books (id),
FOREIGN KEY (user_id) REFERENCES users (id)
);
"""