-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabase.py
More file actions
58 lines (46 loc) · 1.65 KB
/
Database.py
File metadata and controls
58 lines (46 loc) · 1.65 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
"""
Database.py
Handle database operations, create tables, insert, update, delete, checks, etc.
"""
import hashlib
import sqlite3
import random
import os
import config
import logging
class Database:
def __init__(self):
self.logger = logging.getLogger(__name__)
def check_or_init_db(self):
"""
Check if database exists, if not create it
"""
if not os.path.exists(config.DATABASE_FILE):
self.logger.warning(f"Database {config.DATABASE_FILE} does not exist!")
self.create_db()
def check_or_init_tables(self):
"""
Check if tables exist, if not create them
"""
with sqlite3.connect(config.DATABASE_FILE) as db:
cursor = db.cursor()
if not cursor.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='texts';").fetchone():
self.logger.warning("Audio Cache Table does not exist! Creating.")
# Audio Cache Table -~ SELECT id, hash, text, state, audio_file_path FROM texts
cursor.execute("""CREATE TABLE IF NOT EXISTS texts (
id INTEGER PRIMARY KEY AUTOINCREMENT,
hash TEXT,
text TEXT,
state TEXT,
audio_file_path TEXT
);""")
def create_db(self):
"""
(Only) Create empty database
"""
with sqlite3.connect(config.DATABASE_FILE) as db:
cursor = db.cursor()
# Create texts table
cursor.execute("""VACUUM;""")
db.commit()
self.logger.info(f"Database created successfully as {config.DATABASE_FILE}")