From bb8838761df4d67cba6343fd1a65a16844263302 Mon Sep 17 00:00:00 2001 From: Sudeep Virajith Date: Thu, 22 Jun 2023 12:32:07 +0530 Subject: [PATCH] done --- Database.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 Database.py diff --git a/Database.py b/Database.py new file mode 100644 index 0000000..febc02a --- /dev/null +++ b/Database.py @@ -0,0 +1,46 @@ +import json + +import mysql.connector + +def Query(sql): + connection = mysql.connector.connect( + host="YOUR DATABASE HOST", + port="YOUR DATABASE PORT", + user="YOUR DATABASE HOST", + password="YOUR DATABASE PASSWORD", + database="YOUR DATABASE" + ) + cursor = connection.cursor() + + try: + cursor.execute(sql) + connection.commit() + connection.close() + return "done" + except Exception as e: + raise SyntaxError(e) + + +def SelectQuery(sql) -> list: + connection = mysql.connector.connect( + host="YOUR DATABASE HOST", + port="YOUR DATABASE PORT", + user="YOUR DATABASE HOST", + password="YOUR DATABASE PASSWORD", + database="YOUR DATABASE" + ) + dataset = [] + try: + curser = connection.cursor() + curser.execute(sql) + responses = curser.fetchall() + try: + for response in responses: + dataset.append({"id": response[0], "name": response[1].decode("utf-8")}) + except Exception as e: + for response in responses: + dataset.append({"id": response[0], "name": response[1]}) + connection.close() + return dataset + except Exception as e: + raise SyntaxError(e) \ No newline at end of file