-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_interaction.py
213 lines (154 loc) · 5.18 KB
/
db_interaction.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
208
209
210
211
212
213
"""
Created on Apr 11, 2016
Last updated on Apr 13, 2016
Copyright (c) 2016 Teodoro Montanaro and Luigi De Russis
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License
@author: Teodoro Montanaro, Luigi De Russis
"""
import sqlite3
def insert_task(text, urgent):
"""
:param text: text that we want to insert as task in the db
:param urgent: 0 if the task is not urgent, 1 otherwise
Insert a task in the database
"""
# prepare the query
sql = """INSERT INTO task(todo, urgent) VALUES (?, ?)"""
# connect to the db
conn = sqlite3.connect("db/task_list.db")
cursor = conn.cursor()
try:
# execute the query passing the needed parameters
cursor.execute(sql, (text, urgent) )
# commit all pending queries
conn.commit()
except Exception, e:
print str(e)
# if something goes wrong: rollback
conn.rollback()
# close the connection
conn.close()
def update_task(id_task, text, urgent):
"""
:param id_task: it represents the task id of the element we want to update
:param text: text that we want to insert as task in the db
:param urgent: 0 if the task is not urgent, 1 otherwise
Update a task in the database
"""
# prepare the query
sql = """UPDATE task SET todo=?, urgent=? WHERE id_task = ?"""
# connect to the db
conn = sqlite3.connect("db/task_list.db")
cursor = conn.cursor()
try:
# execute the query passing the needed parameters
cursor.execute(sql, (text, urgent,id_task) )
# commit all pending queries
conn.commit()
except Exception, e:
print str(e)
# if something goes wrong: rollback
conn.rollback()
# close the connection
conn.close()
def delete_task(id_task):
"""
:param id_task: it represents the task id of the element we want to delete
Delete a task in the database
"""
# prepare the query
sql = """DELETE FROM task WHERE id_task = ?"""
# connect to the db
conn = sqlite3.connect("db/task_list.db")
cursor = conn.cursor()
try:
# execute the query passing the needed parameters
cursor.execute(sql, (id_task,) )
# commit all pending queries
conn.commit()
except Exception, e:
print str(e)
# if something goes wrong: rollback
conn.rollback()
# close the connection
conn.close()
def get_tasks():
"""
Get existing tasks from the database
"""
tasks = []
sql = "SELECT id_task, todo, urgent FROM task"
conn = sqlite3.connect("db/task_list.db")
# to remove u from sqlite3 cursor.fetchall() results
conn.text_factory = sqlite3.OptimizedUnicode
cursor = conn.cursor()
cursor.execute(sql)
results = cursor.fetchall()
for task in results:
tasks.append(task)
conn.close()
return tasks
def get_filtered_tasks(search_substring):
"""
:param search_substring: it represents the string that will be used as filter for tasks
Get filtered existing tasks from the database
"""
tasks = []
sql = "SELECT id_task, todo, urgent FROM task WHERE todo LIKE ?"
text = "%"+search_substring + "%"
conn = sqlite3.connect("db/task_list.db")
# to remove u from sqlite3 cursor.fetchall() results
conn.text_factory = sqlite3.OptimizedUnicode
cursor = conn.cursor()
cursor.execute(sql, (text, ) )
results = cursor.fetchall()
for task in results:
tasks.append(task)
conn.close()
return tasks
def get_task(id_task):
"""
:param id_task: unique identifier for the task we want to retrieve
Get a specified task from the database
"""
# prepare the query
sql = "SELECT id_task, todo, urgent FROM task WHERE id_task = ?"
# connect to the db
conn = sqlite3.connect("db/task_list.db")
# to remove u from sqlite3 cursor.fetchall() results
conn.text_factory = sqlite3.OptimizedUnicode
cursor = conn.cursor()
cursor.execute(sql, (id_task, ))
task = cursor.fetchone()
# close the connection
conn.close()
return task
def remove_task_by_id(id_task):
"""
:param id_task: unique identifier for the task we want to remove
Remove a specific task from the db
"""
# prepare the query
sql = "DELETE FROM task WHERE id_task = ?"
# connect to the db
conn = sqlite3.connect("db/task_list.db")
cursor = conn.cursor()
try:
# execute the query passing the needed parameters
cursor.execute(sql, (id_task, ))
# commit all pending executed queries in the connection
conn.commit()
except Exception, e:
print str(e)
# if something goes wrong: rollback
conn.rollback()
# close the connection
conn.close()