diff options
author | Vikas Kushwaha <dev@vikas.rocks> | 2024-11-21 13:23:10 +0530 |
---|---|---|
committer | Vikas Kushwaha <dev@vikas.rocks> | 2024-11-21 13:23:10 +0530 |
commit | 8f0751170385989677392f806762a211f99412ef (patch) | |
tree | f09ad917798fa313edb77925a1ddbc6de8fb37ce /test/database.py |
First Commit
Diffstat (limited to 'test/database.py')
-rw-r--r-- | test/database.py | 36 |
1 files changed, 36 insertions, 0 deletions
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) |