import sqlite3

class DataBase:

    def __init__(self):
        # Initilize SqLite
        self.conn = sqlite3.connect("./data/appdata.db")
        self.cursor = self.conn.cursor()

        # Create keystore table if it doesnt exist already
        self.cursor.execute('''
        CREATE TABLE IF NOT EXISTS keystore (
            key TEXT PRIMARY KEY,
            value TEXT
        )
        ''')

    # Set a key in the keystore
    def SetKey(self, Key, Value):
        self.cursor.execute('INSERT OR REPLACE INTO keystore (key, value) VALUES (?, ?)', (Key, Value))
        self.conn.commit()

    # Get a key from the keystore
    def GetKey(self, Key):
        self.cursor.execute('SELECT value FROM keystore WHERE key = ?', (Key, ))
        result = self.cursor.fetchone()
        return result[0] if result else None