-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_interaction.py
41 lines (28 loc) · 1.04 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
"""
pomodoro.py - Utility to time and track pomodoros
db_interaction.py contains the common database interaction code for
pomodoro.py and pomodoro-export.py
"""
import sqlite3 # For database connection
def startup(db_name):
"""Open database, return connection"""
connection = sqlite3.connect(db_name)
cur = connection.cursor()
# Create table on first start
cur.execute("CREATE TABLE IF NOT EXISTS pomodoros"
"(datetime, task, length, complete)")
return connection
def get_data(connection):
"""Convert the data in the database for export"""
cur = connection.cursor()
# Fetch all records from the table
pomo_cursor = cur.execute("SELECT * FROM pomodoros").fetchall()
return pomo_cursor
def insert(connection, row_data):
"""Insert a row into the pomodoros table in the database"""
cur = connection.cursor()
cur.execute("INSERT INTO pomodoros VALUES (?,?,?,?)", row_data)
def shutdown(connection):
"""Safely close database"""
connection.commit()
connection.close()