TRiBByX
10/4/2017 - 1:57 PM

Python Database

Database functions

from handlers import databasehandler
import os

'''
def main():
    database = os.path.join('model/', 'errordb.db')

    sql_create_table = """ CREATE TABLE IF NOT EXISTS errors (
                                id integer PRIMARY KEY,
                                description text,
                                comment text,
                                discovery_date text,
                                last_work_date text,
                                fixed_date text NOT NULL,
                                cause text NOT NULL,
                                discovered_by text,
                                fixed_by text
                            );
                       """
    conn = databasehandler.create_connection(database)

    if conn is not None:
        databasehandler.create_table(conn, sql_create_table)
    else:
        print('An error occured with the creation of the error table')
'''


def main():

    database = os.path.join('model/', 'errordb.db')

    conn = databasehandler.create_connection(database)

    with conn:
        error = ('Something went wrong', 'commenting on this',
                 '04-10-2017', '04-10-2017', '05-10-2017',
                 'Weird Casue', 'Christoffer', 'Christoffer')

        databasehandler.add_error(conn, error)


if __name__ == '__main__':
    main()
import sqlite3
from sqlite3 import Error

DIR = '../model/'


def create_connection(db_file):

    try:
        conn = sqlite3.connect(db_file)
        return conn
    except Error as e:
        print(e)


def create_table(conn, create_table_sql):
    try:
        c = conn.cursor()
        c.execute(create_table_sql)
    except Error as e:
        print(e)


def add_error(conn, error):
    sql = """ INSERT INTO errors(description,
                                 comment, discovery_date,
                                 last_work_date, fixed_date,
                                 cause, discovered_by, fixed_by)
              VALUES(?,?,?,?,?,?,?,?) """

    cur = conn.cursor()
    cur.execute(sql, error)

    return cur.lastrowid