-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.py
103 lines (78 loc) · 2.57 KB
/
database.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
import pathlib
import sqlite3
from sqlite3 import Error
import os
class Database:
def __init__(self, file_name):
# Path to the database file
self.database = os.path.join(str(pathlib.Path().absolute()), "data", file_name + '.db')
self.file_name = file_name
# Keeping connection to the database accessible from everywhere in the class.
self.conn = self.create_connection()
def create_connection(self):
"""
Create a database connection to a SQLite database
"""
conn = None
try:
conn = sqlite3.connect(self.database)
return conn
except Error as e:
print(self.database)
print("create_connection error is saying:")
print(e)
return conn
def select_item(self):
"""
Query tasks by priority
"""
cur = self.conn.cursor()
cur.execute("SELECT * FROM " + self.file_name)
rows = cur.fetchall()
return rows
def select_status(self, name):
"""
Select status. For exemple infected or not. (NOT USED)
Parameters
----------
name: type string: Name of the person, used to access information in the database.
Returns
-------
result: type list: rows of the query result
"""
cur = self.conn.cursor()
# The ? is used to specify that we are using a variable. We reference it when executing.
sql = '''SELECT status FROM vertices WHERE name=?'''
cur.execute(sql, (name,))
result = cur.fetchall()
return result
def delete_item(self, item_value, field):
"""
Delete a task by task id.
Parameters
----------
item_value: The value of the item to remove
field: type string: The column name of the value
Returns
-------
"""
sql = 'DELETE FROM ' + self.file_name + ' WHERE ' + field + '=?'
cur = self.conn.cursor()
cur.execute(sql, (item_value,))
self.conn.commit()
def change_status(self, name, value):
"""
Update the "status" field of item verifying "name" column matches the name variable.
Parameters
----------
name: Matches of the "name" column
value: The new value to set.
Returns
-------
"""
sql = '''UPDATE vertices
SET status = ?
WHERE name = ?'''
cur = self.conn.cursor()
cur.execute(sql, (value, name))
self.conn.commit()