Browse Source

implemented the necessary code for the Populate function to create the database tables and insert the days of the week.

Helmut Pozimski 10 năm trước cách đây
mục cha
commit
c7ff552dd4
1 tập tin đã thay đổi với 44 bổ sung1 xóa
  1. 44 1
      jwmudlib/database.py

+ 44 - 1
jwmudlib/database.py

@@ -54,4 +54,47 @@ class db:
         except sqlite3.OperationalError:
             raise jwmu_exceptions.DataBaseWriteFailed()
         else:
-            self.__connection.commit()
+            self.__connection.commit()
+
+    def Populate(self):
+        """Creates the initial database structure with all the necessary
+        tables.
+
+        """
+        create_weekdays = """CREATE TABLE weekdays(
+        day_id INTEGER PRIMARY KEY NOT NULL,
+        alarm_time TEXT,
+        name TEXT
+        );"""
+        create_categories = """CREATE TABLE categories(
+        category_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
+        name TEXT NOT NULL,
+        alarm_time TEXT
+        );"""
+        create_alarmdays = """CREATE TABLE alarmdays(
+        date TEXT NOT NULL,
+        alarm_time TEXT,
+        category INTEGER DEFAULT NULL,
+        FOREIGN KEY(category) REFERENCES category(category_id),
+        PRIMARY KEY(date, alarm_time)
+        );"""
+        # create the empty tables
+        self.__ExecuteStatement(create_weekdays)
+        self.__ExecuteStatement(create_categories)
+        self.__ExecuteStatement(create_alarmdays)
+        # fill the weekdays table with the existing 7 days of the week,
+        # these are always hardcoded
+        self.__ExecuteStatement("INSERT INTO weekdays (day_id, alarm_time, "
+                                "name) VALUES (1, 'undefined', 'Monday')")
+        self.__ExecuteStatement("INSERT INTO weekdays (day_id, alarm_time, "
+                                "name) VALUES (2, 'undefined', 'Tuesday')")
+        self.__ExecuteStatement("INSERT INTO weekdays (day_id, alarm_time, "
+                                "name) VALUES (3, 'undefined', 'Wednesday')")
+        self.__ExecuteStatement("INSERT INTO weekdays (day_id, alarm_time, "
+                                "name) VALUES (4, 'undefined', 'Thursday')")
+        self.__ExecuteStatement("INSERT INTO weekdays (day_id, alarm_time, "
+                                "name) VALUES (5, 'undefined', 'Friday')")
+        self.__ExecuteStatement("INSERT INTO weekdays (day_id, alarm_time, "
+                                "name) VALUES (6, 'undefined', 'Saturday')")
+        self.__ExecuteStatement("INSERT INTO weekdays (day_id, alarm_time, "
+                                "name) VALUES (7, 'undefined', 'Sunday')")