From 4278443b634c04ee554b0904dd46a38ff7100e6d Mon Sep 17 00:00:00 2001
From: "Anders H. Rebner" <anderhre@stud.ntnu.no>
Date: Mon, 24 Feb 2020 15:23:18 +0100
Subject: [PATCH] #5 Basic database setup ferdig

---
 scripts/.pylintrc   |  1 +
 soitool/database.py | 62 +++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 61 insertions(+), 2 deletions(-)

diff --git a/scripts/.pylintrc b/scripts/.pylintrc
index 7e9ef2c..3da10b2 100644
--- a/scripts/.pylintrc
+++ b/scripts/.pylintrc
@@ -409,6 +409,7 @@ good-names=app,
            k,
            ex,
            Run,
+           f,
            _
 
 # Include a hint for the correct naming format with invalid-name.
diff --git a/soitool/database.py b/soitool/database.py
index d5bbe29..d8e8e0d 100644
--- a/soitool/database.py
+++ b/soitool/database.py
@@ -2,13 +2,16 @@
 import os
 import sqlite3
 
+# Set name and path to (future) database
 DBNAME = 'Database'
 CURDIR = os.path.dirname(__file__)
 DBPATH = os.path.join(CURDIR, DBNAME)
 
 # DDL-statements for creating tables
-CODEBOOK = 'CREATE TABLE CodeBook(Word VARCHAR PRIMARY KEY, Category VARCHAR, Type int DEFAULT 0)'
-CATEGORYWORDS = 'CREATE TABLE CategoryWords(Word VARCHAR PRIMARY KEY, Category VARCHAR)'
+CODEBOOK = 'CREATE TABLE CodeBook' \
+            '(Word VARCHAR PRIMARY KEY, Category VARCHAR, Type int DEFAULT 0)'
+CATEGORYWORDS = 'CREATE TABLE CategoryWords' \
+                '(Word VARCHAR PRIMARY KEY, Category VARCHAR)'
 BYHEART = 'CREATE TABLE ByHeart(Word VARCHAR PRIMARY KEY)'
 
 
@@ -23,11 +26,18 @@ class Database():
             print('connecting to existing DB.')
             self.conn = sqlite3.connect(DBPATH)
             print('DB-connection established.')
+            self.conn.row_factory = sqlite3.Row  # Enables row['columnName']
+
         else:
             print('Creating new DB.')
             self.conn = sqlite3.connect(DBPATH)
             self.create_tables()
             print('DB created.')
+            self.fill_tables()
+            print('Tables filled with data.')
+            self.conn.row_factory = sqlite3.Row  # Enables row['columnName']
+
+        self.categories = self.get_categories()
 
     def create_tables(self):
         """Create tables Codebook, CategoryWords and ByHeart."""
@@ -37,3 +47,51 @@ class Database():
             self.conn.execute(stmt)
 
         self.conn.commit()
+
+    def fill_tables(self):
+        """Fill tables with testdata."""
+        # Read from ByHeart
+        f = open(os.path.join(CURDIR, "testdata/ByHeart.txt"), "r")
+
+        # Loop through words in file and insert them into ByHeart-table
+        for word in f:
+            stmt = 'INSERT INTO ByHeart(Word) VALUES(?)'
+            self.conn.execute(stmt, (word[:-1],))  # Remove \n
+
+        # Read from CategoryWords
+        file_path = os.path.join(CURDIR, "testdata/CategoryWords.txt")
+        f = open(file_path, "r", encoding='utf-8')
+        no_of_categories = int(f.readline()[0])
+
+        for _ in range(no_of_categories):  # Loop through all categories
+            line = f.readline().split(", ")
+
+            category = line[0]
+            no_of_words = int(line[1][:-1])  # Remove \n
+
+            for _ in range(no_of_words):  # Loop through
+                word = f.readline()[:-1]  # Remove \n
+                stmt = 'INSERT INTO CategoryWords(Word, Category) VALUES(?, ?)'
+                self.conn.execute(stmt, (word, category,))
+
+        self.conn.commit()
+
+    def get_categories(self):
+        """
+        Retrieve all categories from table CategoryWords.
+
+        Returns
+        -------
+        List of strings
+            Categories
+        """
+        stmt = 'SELECT Category FROM CategoryWords GROUP BY Category'
+        test = self.conn.execute(stmt)
+        categories = []
+        for row in test:
+            categories.append(row['Category'])
+
+        return categories
+
+
+# temp = Database()
-- 
GitLab