aboutsummaryrefslogtreecommitdiff
path: root/main.py
blob: 261a9fe40a59da2e2fe36b5c7ca8372d05218afb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/bin/python3

from frames import *
from getpass import getuser
import mariadb
#import pdb


fields = (('userid', 'varchar(100)', 'PRIMARY KEY'),
          ('ptype', 'varchar(4)', 'NOT NULL'))


def start():
    with mariadb.connect(host="localhost", user=getuser(), password='') as connection:
        with connection.cursor() as cursor:

            # check if databse exist
            cursor.execute('SHOW DATABASES')
            for dbs in cursor.fetchall():
                if 'ptest' in dbs:
                    break
            else:
                print('Creating database ptest.')
                cursor.execute("CREATE DATABASE ptest")
            cursor.execute("USE ptest")

            # check if table exist and is in proper format
            cursor.execute('SHOW TABLES')
            for tables in cursor.fetchall():
                if 'records' in tables:
                    cursor.execute("DESCRIBE records")
                    columns = cursor.fetchall()
                    l_fields = len(fields)
                    l_columns = len(columns)

                    table_ok = True
                    if l_fields != l_columns:
                        table_ok = False
                    for i in range(l_fields):
                        if fields[i][0] != columns[i][0] or fields[i][1] != columns[i][1]:
                            table_ok = False

                    if table_ok == False:
                        cursor.execute("DROP TABLE records")
                        continue
                    break
            else:
                print('Creating table records.')
                columns = ''
                for i in range(len(fields)):
                    if i == 0:
                        columns += ' '.join(fields[i])
                    else:
                        columns += ', ' + ' '.join(fields[i])
                cursor.execute("CREATE TABLE records({})".format(columns))

            root = Tk()
            root.title("Personality Test")

            WIN_HEIGHT, WIN_WIDTH = 1200, 675
            root.geometry(f"{WIN_HEIGHT}x{WIN_WIDTH}")
            #root.minsize(WIN_HEIGHT, WIN_WIDTH)
            #root.resizable(width=False, height=False)
            root.configure(background="black")
            root.bind_all("<Button-1>", lambda event: event.widget.focus_set())

            #startpage = RecordsPage(HomePage(root, cursor), root, cursor)
            startpage = HomePage(root, cursor)
            startpage.pack(fill=BOTH, expand=TRUE)
            startpage.mainloop()

        connection.commit()


start()

# try:
# except Error as e:
#     messagebox.showerror(title='Oops!', message=e)
#