diff options
-rw-r--r-- | add_group_form.py | 5 | ||||
-rwxr-xr-x | assignment-list.py | 2 | ||||
-rw-r--r-- | db_sqlite.py | 71 | ||||
-rw-r--r-- | globals.py | 2 |
4 files changed, 73 insertions, 7 deletions
diff --git a/add_group_form.py b/add_group_form.py index e62b211..dc83b89 100644 --- a/add_group_form.py +++ b/add_group_form.py @@ -5,6 +5,7 @@ from PyQt5.QtCore import Qt from add_entry_form import Globals from group import Group +DB = __import__("db_sqlite") class addGroupForm(QDialog): """ @@ -65,9 +66,9 @@ class addGroupForm(QDialog): QMessageBox.Close) return + new_id = DB.insertGroup(Group(0, name_text, column_text, link_text)) # TODO do the database stuff (this will allow us to get the id) - Globals.groups.append(Group(Globals.max_group_id, name_text, column_text, link_text)) - Globals.max_group_id = Globals.max_group_id + 1 + Globals.groups.append(Group(new_id, name_text, column_text, link_text)) self.close() if __name__ == "__main__": diff --git a/assignment-list.py b/assignment-list.py index d5d436e..0fac895 100755 --- a/assignment-list.py +++ b/assignment-list.py @@ -51,7 +51,7 @@ class AssignmentList(QMainWindow): def setupDB(self): # TODO should check for existing db instead of blindly creating it - DB.CreateTables("/tmp/test.db") + DB.initDB() def displayWidgets(self): main_widget = QWidget(self) diff --git a/db_sqlite.py b/db_sqlite.py index 9d47da8..99f412b 100644 --- a/db_sqlite.py +++ b/db_sqlite.py @@ -1,13 +1,24 @@ +import os import sys from PyQt5.QtSql import QSqlDatabase, QSqlQuery Globals = __import__("globals") +from group import Group -def CreateTables(db_path): +def initDB(): """ - Create database at a specified directory + Check for existing database. If it doesn't exist, build it + """ + if not os.path.exists(Globals.db_path): + createTables() + + loadFromTables() + +def createTables(): + """ + Create database at a specified Globals.db_path """ database = QSqlDatabase.addDatabase("QSQLITE") # SQlite version 3 - database.setDatabaseName(db_path) + database.setDatabaseName(Globals.db_path) if not database.open(): print("Unable to open data source file.") @@ -42,3 +53,57 @@ def CreateTables(db_path): """) database.close() + +def loadFromTables(): + """ + Load groups and entries into global variables + """ + database = QSqlDatabase.addDatabase("QSQLITE") # SQlite version 3 + database.setDatabaseName(Globals.db_path) + + if not database.open(): + print("Unable to open data source file.") + sys.exit(1) # Error out. TODO consider throwing a dialog instead + + query = QSqlQuery() + + query.exec_("SELECT * FROM groups") + while query.next(): + record = query.record() + Globals.groups.append( + Group( + record.field("id").value(), + record.field("name").value(), + record.field("column").value(), + record.field("link").value())) + + database.close() + +def insertGroup(new_group): + """ + Save groups and entries to the database at Globals.db_path + """ + output = -1 + + database = QSqlDatabase.addDatabase("QSQLITE") # SQlite version 3 + database.setDatabaseName(Globals.db_path) + + if not database.open(): + print("Unable to open data source file.") + sys.exit(1) # Error out. TODO consider throwing a dialog instead + + query = QSqlQuery() + + query.prepare(""" + INSERT INTO groups (name, column, link) VALUES (?, ?, ?) + """) + query.addBindValue(new_group.name) + query.addBindValue(new_group.column) + query.addBindValue(new_group.link) + query.exec_() + + output = query.lastInsertId() + + database.close() + + return output @@ -1,3 +1,3 @@ groups = [] entries = [] -max_group_id = 1 # TODO remove this, only a placeholder until database is implemented +db_path = "/tmp/test.db" |