From a7b4e539df78412fb2b859f99bb018c613654e48 Mon Sep 17 00:00:00 2001 From: William Tran Date: Thu, 11 Mar 2021 07:20:20 -0500 Subject: [PATCH] Adds python file to setup database --- .gitignore | 3 ++- backend/setup_db.py | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 backend/setup_db.py diff --git a/.gitignore b/.gitignore index f5e96db..9069f18 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -venv \ No newline at end of file +venv +links.db \ No newline at end of file diff --git a/backend/setup_db.py b/backend/setup_db.py new file mode 100644 index 0000000..b7ff457 --- /dev/null +++ b/backend/setup_db.py @@ -0,0 +1,35 @@ +import sqlite3 +import os + +DB_PATH = os.path.join(os.path.dirname(__file__), 'links.db') + +con = sqlite3.connect(DB_PATH) + +# array of links to store +links = [ + ('http://csclub.uwaterloo.ca/','CS Club Website',3,0), + ('https://www.instagram.com/uwcsclub/','Instagram',4,1), + ('https://www.facebook.com/uw.computerscienceclub','Facebook',5,2), + ('http://twitch.tv/uwcsclub','Twitch',6,3), + ('http://bit.ly/uwcsclub-yt','YouTube',7,4), +] + +# SQLite setup +cur = con.cursor() + +# test if table already exists +cur.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='links'") +if cur.fetchone(): + raise Exception('Links table already exists.') +else: + cur.execute('''CREATE TABLE links ( + url text NOT NULL, + name text NOT NULL, + clicks int NOT NULL, + position int NOT NULL UNIQUE, + UNIQUE(url, name) + )''') + cur.executemany('INSERT INTO links VALUES (?,?,?,?)', links) + con.commit() + +con.close() \ No newline at end of file