From 8f0751170385989677392f806762a211f99412ef Mon Sep 17 00:00:00 2001 From: Vikas Kushwaha Date: Thu, 21 Nov 2024 13:23:10 +0530 Subject: First Commit --- test/database.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 test/database.py (limited to 'test/database.py') diff --git a/test/database.py b/test/database.py new file mode 100644 index 0000000..38b4f70 --- /dev/null +++ b/test/database.py @@ -0,0 +1,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) -- cgit v1.2.3