Skip to content
Snippets Groups Projects
Commit 4278443b authored by Anders H. Rebner's avatar Anders H. Rebner
Browse files

#5 Basic database setup ferdig

parent 765aea70
No related branches found
No related tags found
1 merge request!10Database setup
......@@ -409,6 +409,7 @@ good-names=app,
k,
ex,
Run,
f,
_
# Include a hint for the correct naming format with invalid-name.
......
......@@ -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()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment