Files
bbbbbs/db_connection.py
2022-12-01 13:13:14 +01:00

61 lines
1.9 KiB
Python
Executable File

#!/usr/bin/python3
import sqlite3
import logging as log
database = "bbbbs.sqlite"
log.basicConfig(level=log.INFO, encoding="utf-8") # set level to log.DEBUG when debugging
try:
sqliteConnection = sqlite3.connect(database)
cursor = sqliteConnection.cursor()
cursor.execute('select sqlite_version();')
log.debug("Sqlite version is %s" % cursor.fetchall())
log.info("connected to %s" % database)
except sqlite3.Error as error:
log.critical('Error occured: ', error)
def create_transmissions_table():
# timeslots is stored as a 10 bit number with each bit representing a specific timeslot.
scheme = """
CREATE TABLE IF NOT EXISTS transmissions (
date TEXT NOT NULL,
author TEXT NOT NULL,
title TEXT NOT NULL,
tranmission_text TEXT NOT NULL,
timeslots INT NOT NULL,
transmission_order INT NOT NULL,
play BOOLEAN NOT NULL
);
"""
if cursor.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='transmissions';").fetchall() == []:
try:
cursor.execute(scheme)
log.info("Successfully created database transmissions")
except sqlite3.Error as error:
log.critical("Couldn't create transmissions table: ", error)
exit(1)
else:
log.debug('DB transmissions exists, not creating.')
def create_timeslots_table():
scheme = """
CREATE TABLE IF NOT EXISTS timeslots (
timeslot INT NOT NULL,
time TEXT NOT NULL,
active BOOLEAN NOT NULL
);
"""
if cursor.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='timeslots';").fetchall() == []:
try:
cursor.execute(scheme)
log.info("Successfully created database transmissions")
except sqlite3.Error as error:
log.critical("Couldn't create timeslots table: ", error)
exit(1)
create_transmissions_table()
create_timeslots_table()
sqliteConnection.close()