From 1d1383ce943cf6b1ddb32b031a9c9a10e29f98fe Mon Sep 17 00:00:00 2001 From: yuzu-eva Date: Tue, 11 Mar 2025 00:20:43 +0100 Subject: first iteration of flask-app; index is working --- database.py | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100755 database.py (limited to 'database.py') diff --git a/database.py b/database.py new file mode 100755 index 0000000..aebd72e --- /dev/null +++ b/database.py @@ -0,0 +1,54 @@ +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)) + -- cgit v1.2.3