aboutsummaryrefslogtreecommitdiff
path: root/test/database.py
blob: 38b4f70d4a348d33eb60586127f0a3d626188746 (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
from tkinter.messagebox import showerror
from getpass import *
from mysql.connector import connect, Error

def initialize_db():

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

            cursor.execute('SHOW DATABASES')
            for dbs in cursor.fetchall():
                if 'ptest' in dbs:
                    break
            else:
                print('createing')
                cursor.execute("CREATE DATABASE ptest")

            cursor.execute("USE ptest")

            cursor.execute('SHOW TABLES')
            for tables in cursor.fetchall():
                if 'records' in tables:
                    break
            else:
                cursor.execute("""CREATE TABLE records(
                id INT(3) AUTO_INCREMENT PRIMARY KEY,
                name VARCHAR(50), type VARCHAR(4)
                )""")

            cursor.execute("DESCRIBE records")
            print(cursor.fetchall())

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