from dataclasses import dataclass import sqlite3 import datetime @dataclass class Database: def __enter__(self): self.__connection = sqlite3.connect("website.db") return self def __exit__(self, type, value, traceback): self.__connection.commit() self.__connection.close() def get_header_links(self): cursor = self.__connection.cursor() cursor.execute("SELECT name, link FROM headerLinks;") return cursor.fetchall() def get_image(self, imageName): cursor = self.__connection.cursor() cursor.execute("SELECT alt, url FROM images WHERE imageName = %s;", (imageName, )) return cursor.fetchone() def get_pfp_images(self): cursor = self.__connection.cursor() cursor.execute("SELECT alt, url FROM images WHERE pfp_img = 1;") return cursor.fetchall() def get_sidebar_images(self): cursor = self.__connection.cursor() cursor.execute("SELECT alt, url FROM images WHERE sidebar_img = 1;") return cursor.fetchall() def get_all_thoughts(self): cursor = self.__connection.cursor() cursor.execute("SELECT id, title, datetime FROM thoughts") return cursor.fetchall() def get_thought(self, id_): cursor = self.__connection.cursor() cursor.execute(""" SELECT title, datetime, markdown_text FROM thoughts WHERE id = ?;""", (id_, )) return cursor.fetchall() def add_thought(self, title, markdown): cursor = self.__connection.cursor() cursor.execute(""" INSERT INTO thoughts (title, datetime, markdown_text) VALUES (?, datetime('now'), ?);""", (title, markdown))