-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdatabase.py
More file actions
81 lines (66 loc) · 1.92 KB
/
database.py
File metadata and controls
81 lines (66 loc) · 1.92 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
import sqlite3
import os
FILENAME = "database.db"
def ensure_tables_are_created():
"""
Initialize the database. This method should be called at least once before actually using the database.
Can safely be called multiple times if necessary.
"""
con = sqlite3.connect(FILENAME)
cur = con.cursor()
cur.execute('''CREATE TABLE IF NOT EXISTS person
(id INTEGER PRIMARY KEY AUTOINCREMENT, name text NOT NULL)''')
con.commit()
con.close()
def get_people():
"""
Get all the content of the database as an array of tuples (id, name).
Return example:
[
(
1,
"Rae"
),
(
2,
"Matt"
)
]
"""
con = sqlite3.connect(FILENAME)
cur = con.cursor()
result = cur.execute("SELECT * FROM person").fetchall()
con.close()
return result
def add_person(name):
"""
Add a person in the collection given a name.
:param: name: The name of the new person to add
:return: The id of the new person
"""
con = sqlite3.connect(FILENAME)
cur = con.cursor()
cur.execute("INSERT INTO person(name) VALUES(?)", [name])
id = cur.lastrowid
con.commit()
con.close()
return id
def delete_person(id):
"""
Deletes a person by id.
:param: id: The person id to delete
:return: True if the person existed and has been succesfully deleted. False otherwise
"""
con = sqlite3.connect(FILENAME)
cur = con.cursor()
result = cur.execute("DELETE FROM person where id = ?", (id,))
deleted = result.rowcount
con.commit()
con.close()
return deleted == 1
def get_db_status():
"""
Returns the status of the database. For simplicity, it only checks if the database file exists
:return True if the database status is correct. False otherwise
"""
return os.path.exists(FILENAME)