#!/usr/bin/python3 import sqlite3 import logging database = "bbbbs.sqlite" logging.basicConfig(level=logging.DEBUG, encoding="utf-8") # set level to log.DEBUG when debugging log = logging.getLogger("db") 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, transmission_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 get_transmissions(date=None, timeslot=None): return cursor.execute("SELECT * FROM transmissions").fetchall() def create_transmission(date, author, title, transmission_text, timeslots, transmission_order, play=True): #(date, author, title, transmission_text, timeslots, transmission_order, play) command=""" INSERT INTO transmissions VALUES(?, ?, ?, ?, %d, %d, %d); """%(timeslots, transmission_order, (1 if play else 0)) try: cursor.execute(command, (date, author, title, transmission_text)) sqliteConnection.commit() log.debug("Created a transmission with date %s, author %s, timeslots %s"%(date, author, bin(timeslots))) except sqlite3.Error as error: log.error("Couldn't create a transmission with date %s, author %s, timeslots %s"%(date, author, bin(timeslots))) log.error(error) def delete_transmission(): pass