aboutsummaryrefslogtreecommitdiff
path: root/test/database.py
diff options
context:
space:
mode:
Diffstat (limited to 'test/database.py')
-rw-r--r--test/database.py36
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)