-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb_interaction.py
More file actions
128 lines (92 loc) · 3.09 KB
/
db_interaction.py
File metadata and controls
128 lines (92 loc) · 3.09 KB
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
"""
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 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_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()