From 88b1ea53f5643206f09d78604535b2917936c9f5 Mon Sep 17 00:00:00 2001 From: Nathan Woodburn Date: Wed, 8 Nov 2023 19:11:55 +1100 Subject: [PATCH] fix: DB table creation with server --- db.py | 3 +-- sites/db.py | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 sites/db.py diff --git a/db.py b/db.py index 8a129b2..668a9fb 100644 --- a/db.py +++ b/db.py @@ -27,8 +27,7 @@ def check_tables(): token VARCHAR(255) NOT NULL, PRIMARY KEY (id) ) - """) - + """) cursor.close() connection.close() diff --git a/sites/db.py b/sites/db.py new file mode 100644 index 0000000..8c60d4c --- /dev/null +++ b/sites/db.py @@ -0,0 +1,39 @@ +import mysql.connector +import os +import dotenv + +dotenv.load_dotenv() + +# Database connection +dbargs = { + 'host':os.getenv('DB_HOST'), + 'user':os.getenv('DB_USER'), + 'password':os.getenv('DB_PASSWORD'), + 'database':os.getenv('DB_NAME') +} + +def check_tables(): + connection = mysql.connector.connect(**dbargs) + cursor = connection.cursor() + cursor.execute(""" + CREATE TABLE IF NOT EXISTS site ( + id INT(11) NOT NULL AUTO_INCREMENT, + domain VARCHAR(255) NOT NULL, + data VARCHAR(2048) NOT NULL, + PRIMARY KEY (id) + ) + """) + + cursor.close() + connection.close() + +def get_site(domain): + connection = mysql.connector.connect(**dbargs) + cursor = connection.cursor() + cursor.execute(""" + SELECT * FROM site WHERE domain = %s + """, (domain,)) + site = cursor.fetchall() + cursor.close() + connection.close() + return site