Skip to content

Commit 7dcd017

Browse files
committed
Added method to create tables in sql
1 parent 5d3afe2 commit 7dcd017

File tree

2 files changed

+177
-10
lines changed

2 files changed

+177
-10
lines changed

setup.py

Lines changed: 114 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,83 @@
11
import check
2-
import sqltables
32

43
import pickle
54
import mysql.connector
65

6+
from mysql.connector import errorcode
77

8+
TABLES = {}
9+
TABLES['employees'] = (
10+
"CREATE TABLE `employees` ("
11+
" `emp_no` int(5) NOT NULL AUTO_INCREMENT,"
12+
" `birth_date` date NOT NULL,"
13+
" `first_name` varchar(15) NOT NULL,"
14+
" `last_name` varchar(15) NOT NULL,"
15+
" `gender` enum('M','F') NOT NULL,"
16+
" `hire_date` date NOT NULL,"
17+
" `age` int(2) NOT NULL,"
18+
" PRIMARY KEY (`emp_no`)"
19+
") ")
20+
21+
TABLES['clients'] = (
22+
"CREATE TABLE `clients` ("
23+
" `acc_no` int(5) NOT NULL AUTO_INCREMENT PRIMARY KEY,"
24+
" `acc_type` enum('S','C') NOT NULL,"
25+
" `first_name` varchar(15) NOT NULL,"
26+
" `last_name` varchar(15) NOT NULL,"
27+
" `gender` enum('M','F') NOT NULL,"
28+
" `birth_date` date NOT NULL,"
29+
" `acc_creation_date` date NOT NULL,"
30+
" `mobile_no` int(10) NOT NULL,"
31+
" `email_id` varchar(25) NOT NULL"
32+
") "
33+
)
34+
35+
TABLES['savings'] = (
36+
"CREATE TABLE `savings` ("
37+
" `acc_no` int(5) NOT NULL,"
38+
" `balance` int NOT NULL,"
39+
" `loan` enum('YES','NO') NOT NULL,"
40+
" PRIMARY KEY (`acc_no`),"
41+
" FOREIGN KEY(`acc_no`) REFERENCES clients(acc_no)"
42+
") "
43+
)
44+
45+
TABLES['current'] = (
46+
"CREATE TABLE `current` ("
47+
" `acc_no` int(5) NOT NULL,"
48+
" `balance` int NOT NULL,"
49+
" `overdraft` int NOT NULL,"
50+
" PRIMARY KEY (`acc_no`),"
51+
" FOREIGN KEY(`acc_no`) REFERENCES clients(acc_no)"
52+
") "
53+
)
54+
55+
TABLES['loan'] = (
56+
"CREATE TABLE `loan` ("
57+
" `acc_no` int(5) NOT NULL,"
58+
" `loan_type` enum('PL','HL','EL','TL','BL') NOT NULL,"
59+
" `loan_amt` int NOT NULL,"
60+
" `time_period_months` int NOT NULL,"
61+
" `iterest_perc_per_annum` int(1) NOT NULL,"
62+
" `amt-per-month` int NOT NULL,"
63+
" `remaining_amt` int NOT NULL,"
64+
" PRIMARY KEY (`acc_no`),"
65+
" FOREIGN KEY(`acc_no`) REFERENCES clients(acc_no)"
66+
") "
67+
)
68+
69+
TABLES['overdraft']=(
70+
"CREATE TABLE `overdraft` ("
71+
" `acc_no` int(5) NOT NULL,"
72+
" `overdraft_amt` int NOT NULL,"
73+
" `od_with_interest_remaining` int NOT NULL,"
74+
" PRIMARY KEY (`acc_no`),"
75+
" FOREIGN KEY(`acc_no`) REFERENCES clients(acc_no)"
76+
") "
77+
)
78+
79+
80+
############################################################################################
881
query=""
982
Password=""
1083
Database=""
@@ -41,10 +114,24 @@ def querycheck():
41114
if conn!="":
42115
if conn.is_connected:
43116
print("Connection established successfully.")
44-
with open("firsttime.txt","w") as f:
45-
f.write("False")
46-
sqltables.tables()
47-
ans=True
117+
if check.check()==True:
118+
cursor=conn.cursor()
119+
#Table creation
120+
for table_name in TABLES:
121+
table_description = TABLES[table_name]
122+
try:
123+
print("Creating table {}: ".format(table_name), end='')
124+
cursor.execute(table_description)
125+
except mysql.connector.Error as err:
126+
if err.errno == errorcode.ER_TABLE_EXISTS_ERROR:
127+
print("already exists.")
128+
else:
129+
print(err.msg)
130+
else:
131+
print("OK")
132+
with open("firsttime.txt","w") as f:
133+
f.write("False")
134+
ans=True
48135

49136
if not ans:
50137
print("There was a problem in connection")
@@ -83,4 +170,25 @@ def setup():
83170
else:
84171
if querycheck():
85172
connectionquery()
86-
setup()
173+
174+
"""def tables():
175+
conn=setup()
176+
cursor=conn.cursor()
177+
for table_name in TABLES:
178+
table_description = TABLES[table_name]
179+
try:
180+
print("Creating table {}: ".format(table_name), end='')
181+
cursor.execute(table_description)
182+
except mysql.connector.Error as err:
183+
if err.errno == errorcode.ER_TABLE_EXISTS_ERROR:
184+
print("already exists.")
185+
else:
186+
print(err.msg)
187+
else:
188+
print("OK")"""
189+
190+
191+
##################################################################################################
192+
#Links
193+
#https://education.github.com/git-cheat-sheet-education.pdf
194+
#https://dev.mysql.com/doc/connector-python/en/connector-python-example-ddl.html

sqltables.py

Lines changed: 63 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,81 @@
1515
" `last_name` varchar(15) NOT NULL,"
1616
" `gender` enum('M','F') NOT NULL,"
1717
" `hire_date` date NOT NULL,"
18-
" `age` int(2) NOT NULL"
18+
" `age` int(2) NOT NULL,"
1919
" PRIMARY KEY (`emp_no`)"
2020
") ")
2121

22-
TABLES['client'] = (
22+
TABLES['clients'] = (
2323
"CREATE TABLE `clients` ("
2424
" `acc_no` int(5) NOT NULL AUTO_INCREMENT,"
25-
" `acc_type` enum('S','C') NOT NULL"
25+
" `acc_type` enum('S','C') NOT NULL,"
2626
" `first_name` varchar(15) NOT NULL,"
2727
" `last_name` varchar(15) NOT NULL,"
2828
" `gender` enum('M','F') NOT NULL,"
2929
" `birth_date` date NOT NULL,"
3030
" `acc_creation_date` date NOT NULL,"
31+
" `mobile_no int(10) NOT NULL`,"
32+
" `email_id varchar(25) NOT NULL`,"
3133
" PRIMARY KEY (`acc_no`)"
34+
") "
3235
)
3336

37+
TABLES['savings'] = (
38+
"CREATE TABLE `savings` ("
39+
" `acc_no` int(5) NOT NULL,"
40+
" `balance` int NOT NULL,"
41+
" `loan` enum('YES','NO') NOT NULL,"
42+
" PRIMARY KEY (`acc_no`),"
43+
" FOREIGN KEY(`acc_no`) REFERENCES clients(acc_no)"
44+
") "
45+
)
46+
47+
TABLES['current'] = (
48+
"CREATE TABLE `current` ("
49+
" `acc_no` int(5) NOT NULL,"
50+
" `balance` int NOT NULL,"
51+
" `overdraft` int NOT NULL,"
52+
" PRIMARY KEY (`acc_no`),"
53+
" FOREIGN KEY(`acc_no`) REFERENCES clients(acc_no)"
54+
") "
55+
)
56+
57+
TABLES['loan'] = (
58+
"CREATE TABLE `loan` ("
59+
" `acc_no` int(5) NOT NULL,"
60+
" `loan_type` enum('PL','HL','EL','TL','BL') NOT NULL,"
61+
" `loan_amt` int NOT NULL,"
62+
" `time_period_months` int NOT NULL,"
63+
" `iterest_perc_per_annum` int(1) NOT NULL,"
64+
" `amt-per-month` int NOT NULL,"
65+
" `remaining_amt` int NOT NULL,"
66+
" PRIMARY KEY (`acc_no`),"
67+
" FOREIGN KEY(`acc_no`) REFERENCES clients(acc_no)"
68+
") "
69+
)
70+
71+
TABLES['overdraft']=(
72+
"CREATE TABLE `overdraft` ("
73+
" `acc_no` int(5) NOT NULL,"
74+
" `overdraft_amt` int NOT NULL,"
75+
" `od_with_interest_remaining int NOT NULL,"
76+
" PRIMARY KEY (`acc_no`),"
77+
" FOREIGN KEY(`acc_no`) REFERENCES clients(acc_no)"
78+
") "
79+
)
80+
def tables():
81+
for table_name in TABLES:
82+
table_description = TABLES[table_name]
83+
try:
84+
print("Creating table {}: ".format(table_name), end='')
85+
cursor.execute(table_description)
86+
except mysql.connector.Error as err:
87+
if err.errno == errorcode.ER_TABLE_EXISTS_ERROR:
88+
print("already exists.")
89+
else:
90+
print(err.msg)
91+
else:
92+
print("OK")
93+
#Credits
3494
#https://education.github.com/git-cheat-sheet-education.pdf
3595
#https://dev.mysql.com/doc/connector-python/en/connector-python-example-ddl.html
36-

0 commit comments

Comments
 (0)