LinkList/backend/setup_db.py

37 lines
965 B
Python
Raw Permalink Normal View History

2021-03-11 07:20:20 -05:00
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',0,0,1),
('https://www.instagram.com/uwcsclub/','Instagram',0,1,1),
('https://www.facebook.com/uw.computerscienceclub','Facebook',0,2,1),
('http://twitch.tv/uwcsclub','Twitch',0,3,1),
('http://bit.ly/uwcsclub-yt','YouTube',0,4,1),
2021-03-11 07:20:20 -05:00
]
# 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,
2021-03-11 07:20:20 -05:00
position int NOT NULL UNIQUE,
2021-04-02 23:18:05 -04:00
active int NOT NULL,
2021-03-11 07:20:20 -05:00
UNIQUE(url, name)
)''')
2021-04-02 23:18:05 -04:00
cur.executemany('INSERT INTO links VALUES (?,?,?,?,?)', links)
2021-03-11 07:20:20 -05:00
con.commit()
con.close()