-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
95 lines (84 loc) · 2.3 KB
/
database.py
File metadata and controls
95 lines (84 loc) · 2.3 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
import sqlite3
from sqlite3 import Error
def create_connection(db_file) :
conn = None
try:
conn = sqlite3.connect(db_file)
except Error as e:
print(e)
create_tables(conn)
return conn
def create_tables(conn) :
tablitas = [""" CREATE TABLE IF NOT EXISTS events (
slug text PRIMARY KEY,
complete integer
);"""]
if conn is not None:
for tablita in tablitas :
try:
c = conn.cursor()
c.execute(tablita)
except Error as e:
print(e)
else:
print("Error! cannot create the database connection.")
def drop_tables(conn) :
tablitas = ["DROP TABLE events;"]
if conn is not None:
for tablita in tablitas :
try:
c = conn.cursor()
c.execute(tablita)
except Error as e:
print(e)
create_tables(conn)
else:
print("Error! cannot create the database connection.")
def complete_events(conn) :
cur = conn.cursor()
cur.execute("""
SELECT slug FROM events
WHERE complete = 1""")
rows = cur.fetchall()
rows = [str(r[0]) for r in rows]
rows.sort()
return rows
def pending_events(conn) :
cur = conn.cursor()
cur.execute("""
SELECT slug FROM events
WHERE complete = 0""")
rows = cur.fetchall()
rows = [str(r[0]) for r in rows]
rows.sort()
return rows
def event_exists(conn, slug) :
cur = conn.cursor()
cur.execute("""
SELECT slug FROM events
WHERE slug = ?""", (slug,))
rows = cur.fetchall()
return len(rows) > 0
def insert_events(conn, slugs) :
sql = ''' INSERT INTO events (slug, complete)
VALUES(?,0) '''
cur = conn.cursor()
cur.executemany(sql, [(slug,) for slug in slugs])
conn.commit()
def complete_event(conn, slug) :
sql = ''' UPDATE events
SET complete = 1
WHERE slug = ?'''
cur = conn.cursor()
cur.execute(sql, (slug,))
conn.commit()
def set_all_pending(conn) :
sql = ''' UPDATE events
SET complete = 0'''
cur = conn.cursor()
cur.execute(sql)
conn.commit()
if __name__ == "__main__" :
conn = create_connection("molly.db")
insert_events(conn, ["perro", "gato"])
print(pending_events(conn))