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)