LinkList/backend/setup_db.py

36 lines
958 B
Python

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,1,0),
('https://www.instagram.com/uwcsclub/','Instagram',4,1,1),
('https://www.facebook.com/uw.computerscienceclub','Facebook',5,0,2),
('http://twitch.tv/uwcsclub','Twitch',6,0,3),
('http://bit.ly/uwcsclub-yt','YouTube',7,1,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():
print('Links table already exists.')
else:
cur.execute('''CREATE TABLE links (
url text NOT NULL,
name text NOT NULL,
clicks int NOT NULL,
active int NOT NULL,
position int NOT NULL UNIQUE,
UNIQUE(url, name)
)''')
cur.executemany('INSERT INTO links VALUES (?,?,?,?,?)', links)
con.commit()
con.close()