From 1dac1ee189bdcf70a5dd933d5f6a69a15d5a8d7e Mon Sep 17 00:00:00 2001 From: jwansek Date: Tue, 2 Feb 2021 12:24:06 +0000 Subject: added the program files for the first time --- database.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 database.py (limited to 'database.py') diff --git a/database.py b/database.py new file mode 100644 index 0000000..05e8a7e --- /dev/null +++ b/database.py @@ -0,0 +1,33 @@ +import pymysql +import app + +class Database: + def __enter__(self): + self.__connection = pymysql.connect( + **app.CONFIG["mysql"], + charset = "utf8mb4" + ) + return self + + def __exit__(self, type, value, traceback): + self.__connection.close() + + def get_header_links(self): + with self.__connection.cursor() as cursor: + cursor.execute("SELECT name, link FROM headerLinks ORDER BY name;") + return cursor.fetchall() + + def get_image(self, imageName): + with self.__connection.cursor() as cursor: + cursor.execute("SELECT alt, url FROM images WHERE imageName = %s;", (imageName, )) + return cursor.fetchone() + + def get_header_articles(self): + with self.__connection.cursor() as cursor: + cursor.execute("SELECT articleName, link FROM headerArticles;") + return cursor.fetchall() + + +if __name__ == "__main__": + with Database() as db: + print(db.get_header_articles()) \ No newline at end of file -- cgit v1.2.3