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

Database tilkobles og tabeller lages

parent 32eb76e3
No related branches found
No related tags found
1 merge request!10Database setup
Pipeline #69843 failed
"""Database."""
import os
import sqlite3
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)'
BYHEART = 'CREATE TABLE ByHeart(Word VARCHAR PRIMARY KEY)'
class Database():
"""Holds connection to database."""
def __init__(self):
"""Connect to existing db if found, creates new db if not."""
db_exists = os.path.exists(DBPATH)
if db_exists:
print('connecting to existing DB.')
self.conn = sqlite3.connect(DBPATH)
print('DB-connection established.')
else:
print('Creating new DB.')
self.conn = sqlite3.connect(DBPATH)
self.create_tables()
print('DB created.')
def create_tables(self):
"""Create tables Codebook, CategoryWords and ByHeart."""
stmts = [CODEBOOK, CATEGORYWORDS, BYHEART]
for stmt in stmts:
self.conn.execute(stmt)
self.conn.commit()
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