Files
bbbbbs/db.py
2022-12-02 12:31:41 +01:00

60 lines
2.0 KiB
Python
Executable File

#!/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():
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 TEXT 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):
command="""
INSERT INTO transmissions VALUES(?, ?, ?, ?, ?, %d, %d);
"""%(transmission_order, (1 if play else 0))
try:
cursor.execute(command, (date, author, title, transmission_text, timeslots))
sqliteConnection.commit()
log.debug("Created a transmission with date %s, author %s, timeslots %s"%(date, author, 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