Skip to content

Commit

Permalink
Add a class with the first sqlite implementetions (fix #30)
Browse files Browse the repository at this point in the history
  • Loading branch information
EMaksy committed Jun 7, 2021
1 parent 6fdbaff commit a3d746d
Showing 1 changed file with 124 additions and 0 deletions.
124 changes: 124 additions & 0 deletions bin/database.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
# import sqlite
import sqlite3
from sqlite3 import Error

# create a database


def create_sqlite_database(path):
"""
Connect to a database by given a path as paramater
:param str path: String with the path + name of the database
"""
connection = None
try:
connection = sqlite3.connect(path)
print("Connection to SQLite DB successful")
except Error as e:
print(f"The error '{e}' occurred")
return connection

# create a database execute query function


def execute_query(connection, query):
cursor = connection.cursor()
try:
cursor.execute(query)
connection.commit()
print("Query executed successfully")
except Error as e:
print(f"The error '{e}' occurred")

# read a database


def execute_read_query(connection, query):
cursor = connection.cursor()
result = None
try:
cursor.execute(query)
result = cursor.fetchall()
return result
except Error as e:
print(f"The error '{e}' occurred")


def create_empty_database(database):
create_users_table_team = """
CREATE TABLE IF NOT EXISTS team (
TEAM_ID INTEGER PRIMARY KEY AUTOINCREMENT,
TEAM_NUMBER INTEGER NOT NULL,
TEAM_NAME TEXT NOT NULL,
TEAM_START TEXT NOT NULL,
TEAM_END TEXT NOT NULL
);
"""
create_users_table_trainee = """
CREATE TABLE IF NOT EXISTS trainee (
TRAINEE_ID INTEGER PRIMARY KEY AUTOINCREMENT,
START_YEAR TEXT NOT NULL,
GRADUATION_YEAR INTEGER NOT NULL,
DATABASE_VERSION TEXT,
NUMBER_OF_TEAMS TEXT INTEGER,
DURATION NOT NULL INTEGER,
TEAM_ID NOT NULL INTEGER
FOREIGN KEY(TEAM_ID) REFERENCES
);
"""
create_users_table_entry = """
CREATE TABLE IF NOT EXISTS entry (
ENTRY_ID INTEGER PRIMARY KEY AUTOINCREMENT,
ENTRY_TXT TEXT NOT NULL,
ENTRY_DATE TEXT NOT NULL ,
TRAINEE_ID INTEGER NOT NULL,
FOREIGN KEY(TRAINEE_ID) REFERENCES trainee(TRAINEE_ID)
);
"""

# execute querys
execute_query(database, create_users_table_team)
execute_query(database, create_users_table_trainee)
execute_query(database, create_users_table_entry)


class Database():
#
VERSION = 10

def __init__(self, path: str):

self.path = path
# Create database sqlite....
create_database = create_sqlite_database("database")
# create structure of database with required tables
create_empty_database(create_database)
# table entry, trainee and team has been created

def new_day(self):
"""
"""
sql = """CREATE DATABASE"""
self._execute_sql(sql)
return 0
# return an object for example...

def add_entry(self, day_id, entry_txt, entry_date):
"""
"""
return 0
# here we can return an object with which we can work on

def _execute_sql(self, sql_command):
"""
"""
# sql methods should be private
# _execute_sql commands


path = "database.sqlite"
db = Database(path)
day = db.new_day()
text = "text_message"
date = "DATE"
db.add_entry(day, text, date)

0 comments on commit a3d746d

Please sign in to comment.