create a db

This commit is contained in:
2022-12-01 13:13:14 +01:00
parent adfcaf3701
commit 2d537660fa
2 changed files with 60 additions and 1 deletions

60
db_connection.py Executable file
View File

@ -0,0 +1,60 @@
#!/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()

View File

@ -81,7 +81,6 @@ class MainWindow(Gtk.Window):
self.timeslots = Gtk.ListBox() self.timeslots = Gtk.ListBox()
for slot in ["09:00", "15:00", "21:00"]: for slot in ["09:00", "15:00", "21:00"]:
print(slot)
row = Gtk.ListBoxRow() row = Gtk.ListBoxRow()
hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=50) hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=50)
row.add(hbox) row.add(hbox)