diff options
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)) + |
