-
Notifications
You must be signed in to change notification settings - Fork 0
/
DBconnect.py
36 lines (32 loc) · 1.28 KB
/
DBconnect.py
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
import mysql.connector
def create_table(username, pwd):
check_for_table_query = "show tables;"
create_table_query = '''CREATE TABLE time_zones (
India VARCHAR(30) NOT NULL DEFAULT '',
USA VARCHAR(30) NOT NULL DEFAULT '',
UK VARCHAR(30) NOT NULL DEFAULT '',
Australia VARCHAR(30) NOT NULL DEFAULT ''
);'''
try:
db = mysql.connector.connect(
user=username, host='localhost', password=pwd, database='AnalogClockDB')
print(db)
cursor = db.cursor()
cursor.execute(check_for_table_query)
result = cursor.fetchall()
# if table doesn't exist
if len(result) == 0:
try:
cursor.execute(create_table_query)
print("\N{THUMBS UP SIGN} Table Created Successfully!")
except:
db.rollback()
print("Please, try again! Some error occurred")
else:
print("\N{THUMBS DOWN SIGN} Table already exists!!")
except:
db.rollback()
print("DB Connection Error occured")
finally:
db.close()
create_table("root", "christmas")