diff options
| author | yuzu-eva <cafebabe@disroot.org> | 2025-03-11 00:20:43 +0100 |
|---|---|---|
| committer | yuzu-eva <cafebabe@disroot.org> | 2025-03-11 00:20:43 +0100 |
| commit | 1d1383ce943cf6b1ddb32b031a9c9a10e29f98fe (patch) | |
| tree | afd3bd4627ecfcfac6e16714eecb6e4516df7485 /database.py | |
| parent | 85f1c648e09e200a11ae442c8b284b24818f9239 (diff) | |
first iteration of flask-app; index is working
Diffstat (limited to 'database.py')
| -rwxr-xr-x | database.py | 54 |
1 files changed, 54 insertions, 0 deletions
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)) + |
