forked from therippa/notify_labor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
39 lines (29 loc) · 1.08 KB
/
utils.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
import os
import sqlite3
DB_NAME = 'numbers.db'
SCHEMA = 'create table numbers (id integer primary key autoincrement not null,' \
'number text not null,text integer not null)'
def init_db():
exists = os.path.exists(DB_NAME)
with sqlite3.connect(DB_NAME) as conn:
if not exists:
conn.executescript(SCHEMA)
conn.commit()
def insert_to_db(number: str, text: bool):
with sqlite3.connect(DB_NAME) as conn:
cursor = conn.cursor()
cursor.execute('SELECT id FROM numbers WHERE number = ?', (number,))
data = cursor.fetchone()
if not data:
text = 1 if text else 0
cursor.execute('INSERT INTO numbers (number, text) values (?,?)', (number, text))
conn.commit()
def get_all_numbers():
with sqlite3.connect(DB_NAME) as conn:
cursor = conn.cursor()
cursor.execute('SELECT number, text FROM numbers')
numbers = cursor.fetchall()
return numbers
def make_recordings_directory():
if not os.path.exists('recordings'):
os.mkdir('recordings')